Consulting

Results 1 to 12 of 12

Thread: Error 1004

  1. #1

    Question Error 1004

    I am trying to create a column "H" that will display the values in column F unless F is empty. In such a case, the value from column E will be entered into column H, however I am new to using vba and am not sure how to fix this macro as it continues to fail on the first line "If Range("Fi").Value = Empty Then".
    Any advice would be greatly appreciated!

    Sub AdditionalColumn()
    Dim i As Integer
    i = 2
    Do:
    If Range("Fi").Value = Empty Then
        Range("Hi").Value = Range("Ei").Value
        Else: Range("Hi").Value = Range("Fi").Value
    End If
    i = i + 1
    Loop
    End Sub
    Last edited by Paul_Hossler; 01-30-2017 at 03:57 PM. Reason: Added [CODE] tags - please use the [#] icon next time

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum.
    pls use code tags when posting your code. see my signature.

    Range("F" & i), Range("H" & i), etc
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Thank you for your reply!
    I tried what you suggested and now I am getting an error that says:
    "Run-time error '424':
    Object required"

  4. #4
    Sub AdditionalColumn()
    Dim i As Integer
    i = 2
    Do:
    If Range("F" & i).Value.Select = Empty Then
        Range("H" & i).Value = Range("E" & i).Value
        Else: Range("H" & i).Value = Range("F" & i).Value
    End If
    i = i + 1
    Loop
    End Sub
    Last edited by Paul_Hossler; 01-30-2017 at 03:58 PM. Reason: No spaces at the brackets - use the [#] icon, its easier

  5. #5
    VBAX Regular burgDD's Avatar
    Joined
    Jan 2017
    Location
    Atlanta
    Posts
    6
    Location
    This issue may occur if either of the following conditions is true:
    The Microsoft Visual Basic for Applications (VBA) macro copies and pastes one whole row in an Excel 2003 workbook.
    The Microsoft VBA macro copies and pastes a range of 2,516 rows or more rows in an Excel 2003 workbook.

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    why did you insert Select after Value?
    i strongly recommend you take some trainig on vba before using it. check vbaexpress training pages or other web stuff.
    that loop takes you to the bottom of the sheet and errors out at that point. i did'nt notice it at first.

    Sub AdditionalColumn()
    Dim i As Long
    For i = 2 To Range("F" & Rows.Count).End(xlUp).Row
         If Range("F" & i).Value = "" Then
            Range("H" & i).Value = Range("E" & i).Value
            Else
            Range("H" & i).Value = Range("F" & i).Value
         End If
     Next i
     End Sub
    Last edited by Aussiebear; 04-09-2023 at 05:05 PM. Reason: Reduce whitespace in code
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    Thank you so much, I've asked this question on other forums and I have received many replies that have completely changed my original coding, but your changes all make sense to me. I am in the midst of training in vba but do not have a support system which is why I am asking online forums for assistance.
    I created this macro to have a relative reference, but because the output value is "H", the output is always in that column. What could I set "H" equal to so that the output would be relative?

    Thank you!

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    glad to hear about vba training.

    can you elaborate the relative reference?
    relative to what?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Cells(Row Index, Column Index)

    Column H is 8th column. when you refer to a cell in Column H its column index is 8


    using another variable for column reference

    Sub AdditionalColumn()
    Dim i As Long, j As Long
    j = 8
    For i = 2 To Cells(Rows.Count, j - 2).End(xlUp).Row
        If Cells(i, j - 2).Value = "" Then
           Cells(i, j).Value = Cells(i, j - 3).Value
           Else
           Cells(i, j).Value = Cells(i, j - 2).Value
       End If
    Next i
    End Sub
    j = 8 means Column is H in this case.
    j - 2 is Column F
    j - 3 is Column E


    for Column N (instead of column H) for instance, change j = 8 to j = 14

    j = 11 means Column is N
    j - 2 is Column L
    j - 3 is Column K

    each iteration of i increments the row index by 1 in Cells(i, j).
    Last edited by Aussiebear; 04-09-2023 at 05:06 PM. Reason: Reduce whitespace in code
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  10. #10
    Thank you! What you supplied did not quite work, but I adjusted it to what I need. Thank you for all of your assistance!

  11. #11
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    post your solution in case someone will need it in the future and mark the thread solved from threadtools please.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  12. #12
    Sub Relative_Additional_Column()
    Dim i As Long
    Dim j As Long
     j = 11
    For i = 2 To Cells(Rows.Count, j - 2).End(xlUp).Row
         If Cells(i, j - 1).Value = Empty Then
            Cells(i, j).Value = Cells(i, j - 2).Value
            Else
            Cells(i, j).Value = Cells(i, j - 1).Value
        End If
    Next i
    End Sub
    j has to be set to whatever column chosen, and the two other columns are simply subtract or add how many columns away they are, relative to j.
    Last edited by Paul_Hossler; 02-03-2017 at 08:42 AM. Reason: Fixed [ CODE ] to remove spaces so that they work

Tags for this Thread

Posting Permissions

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