Consulting

Results 1 to 5 of 5

Thread: Simple and Basic VBA question.

  1. #1
    VBAX Newbie
    Joined
    May 2019
    Posts
    2
    Location

    Simple and Basic VBA question.

    I want to now create a table (Table8) that basically filters through the other table (Table7) and copy certain cells of each row on this new table.
    The code is working up to a certain extent. I can get the first cell of each row to be copied, however not the rest of the cells of the row.
    I believe the problem is with ".Range(2).Value = Cells(cell.Row, 3).Value" and the other similar lines.

    Sub Optimum() Dim myTable1 As ListObject Dim myArray1 As Variant Dim myTable2 As ListObject Dim myArray2 As Variant Dim newrow As ListRow Set myTable1 = Sheets("Mod 5 CR").ListObjects("Table8") Set myTable2 = Sheets("Sheet5").ListObjects("Table7") Set myArray1 = myTable2.ListColumns(1).Range For Each cell In myArray1 If cell.Value >= Sheets("Mod 5 CR").Range("B20").Value And cell.Value <= Sheets("Mod 5 CR").Range("B18").Value Then Set newrow = myTable1.ListRows.Add With newrow .Range(1).Value = cell.Value .Range(2).Value = Cells(cell.Row, 3).Value .Range(3).Value = Cells(cell.Row, 8).Value .Range(4).Value = Cells(cell.Row, 23).Value .Range(5).Value = Cells(cell.Row, 4).Value End With End If Next cell End Sub

    Thanks guys!Edit: I tried a line "msgbox Cells(cell.Row, 3).Value" and the msgbox displayed the value I wanted!


  2. #2
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi mannygcg!
    It seemed no problem. Please upload the attachment.

  3. #3
    VBAX Newbie
    Joined
    May 2019
    Posts
    2
    Location
    Quote Originally Posted by 大灰狼1976 View Post
    Hi mannygcg!
    It seemed no problem. Please upload the attachment.
    But it does not work :/
    What attachment? of the file?

  4. #4
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Yes, upload the workbook sample please.

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    A workbook would be helpful.
    Maybe, because lines such as:
    .Range(2).Value = Cells(cell.Row, 3).Value
    have ranges without a sheet qualification the wrong cell values could be being copied. Currently the reference defaults to the active sheet cells (or, if the code is in a sheet's code-module, the cells on that sheet).
    A guess:
    .Range(2).Value = Sheets("Sheet5").Cells(cell.Row, 3).Value
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •