jdub21

New Member
Joined
Sep 19, 2022
Messages
30
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
When I run my macro based on conditional formatting, it just adds to the bottom when i need it to post in A8. (I highlighted the cells in yellow to show the duplication)


New Vendors by State.xlsm
C
25
Onboarded
 
Joe - Thanks for the insight. I have been buried in macro code for the past few years and this one has me stumped. I have dissected other .xlsm files that I have worked with and do the same thing but have found that they are written to replace the file and not just the cell in that worksheet which seemed odd.

Basically I have a workbook that will have 50 tabs (one for each state) and on these sheets, I will have conditional formatting based on their stage in the onboarding process. I want to be able to pull all of the "Green" into one summary tab when I click on the "refresh" button...... but instead of it going to the bottom or next available row, I want it to start in a specific cell "A8"
I would recommend first clearing all the old records from your destination (from row 8 down to the end).
Then the code should put the first one in row 8, and keep working down from there.
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Joe - That is my dilemma. When I refresh the summary sheet, it just adds to the bottom as you can see in the screenshot. I just want it to paste in "A8" everytime no matter what.
 

Attachments

  • Screen Shot 2022-09-22 at 9.54.38 AM.png
    Screen Shot 2022-09-22 at 9.54.38 AM.png
    195.3 KB · Views: 3
Upvote 0
Joe - That is my dilemma. When I refresh the summary sheet, it just adds to the bottom as you can see in the screenshot. I just want it to paste in "A8" everytime no matter what.
Yes, I am telling you that you need to update your VBA code, specifically this part here, which is finding the last row on that sheet, and pasting below it:
Rich (BB code):
TransIDCell.Resize(1, 12).Copy Destination:= _
    HTransWS.Range("A1").Offset(HTransWS.Rows.Count - 1, 0).End(xlUp).Offset(1, 0)

So you would have code below you start the loop that deletes all data in rows 8 and below from that sheet, like this:
VBA Code:
'   Find last row with data in column A on HTransWS sheet
    lr = HTransWS.Cells(Rows.Count, "A").End(xlUp).Row

'   Delete data from row 8 down
    If lr >= 8 Then
        HTransWS.Range("A8:A" & lr).EntireRow.Delete
    End If
 
Upvote 0
Solution
I am getting a Compile error: "Next Without For". when I try to run.

---------


Sub CopyOnboarded()

Dim TransIDField As Range
Dim TransIDCell As Range
Dim ATransWS As Worksheet
Dim HTransWS As Worksheet

Set ATransWS = Worksheets("AL")
Set TransIDField = ATransWS.Range("A2", ATransWS.Range("A2").End(xlDown))
Set HTransWS = Worksheets("Onboarded")


For Each TransIDCell In TransIDField

If TransIDCell.Interior.Color = RGB(198, 239, 206) Then

'TransIDCell.Resize(1, 12).Copy Destination:= _
'HTransWS.Range("A1").Offset(HTransWS.Rows.Count - 1, 0).End(xlUp).Offset(1, 0)



' Find last row with data in column A on HTransWS sheet
lr = HTransWS.Cells(Rows.Count, "A").End(xlUp).Row

' Delete data from row 8 down
If lr >= 8 Then
HTransWS.Range("A8:A" & lr).EntireRow.Delete
End If


Next TransIDCell

HTransWS.Columns.AutoFit

End Sub
 
Upvote 0
I said to put the code BEFORE your loop!
So the whole block of code I gave you needs to go above/before this line:
VBA Code:
For Each TransIDCell In TransIDField
 
Upvote 0
Joe - My apologies for being a severe pain in the backside...... So I have moved before the loop. Now do I leave in the following as well or rip that out?

TransIDCell.Resize(1, 12).Copy Destination:= _
HTransWS.Range("A1").Offset(HTransWS.Rows.Count - 1, 0).End(xlUp).Offset(1, 0)


----------------------

Here is the new code.


Sub CopyOnboarded()

Dim TransIDField As Range
Dim TransIDCell As Range
Dim ATransWS As Worksheet
Dim HTransWS As Worksheet

Set ATransWS = Worksheets("AL")
Set TransIDField = ATransWS.Range("A2", ATransWS.Range("A2").End(xlDown))
Set HTransWS = Worksheets("Onboarded")

' Find last row with data in column A on HTransWS sheet
lr = HTransWS.Cells(Rows.Count, "A").End(xlUp).Row

' Delete data from row 8 down
If lr >= 8 Then
HTransWS.Range("A8:A" & lr).EntireRow.Delete
End If

For Each TransIDCell In TransIDField

If TransIDCell.Interior.Color = RGB(198, 239, 206) Then

TransIDCell.Resize(1, 12).Copy Destination:= _
HTransWS.Range("A1").Offset(HTransWS.Rows.Count - 1, 0).End(xlUp).Offset(1, 0)



Next TransIDCell

HTransWS.Columns.AutoFit

End Sub
 
Upvote 0
Yes.
Try it out and see if it works!
 
Upvote 0
I still get the Compile error: "Next without For"
 

Attachments

  • Screenshot 2022-09-22 103216.png
    Screenshot 2022-09-22 103216.png
    52.5 KB · Views: 5
Upvote 0
Why did you remove the "End If" you had in your original Loop before the "Next TransIDCell" line (see the code you originally posted in post 6)?
Nothing should change in your code between the "For" and "Next" lines. Keep what you originally had there.
You are just pasting the block of code I gave you BEFORE the "For" line.
 
Upvote 0
Here is the new code. No errors but does not paste anything in "A8" in fact nothing gets pasted.



Sub CopyOnboarded()

Dim TransIDField As Range
Dim TransIDCell As Range
Dim ATransWS As Worksheet
Dim HTransWS As Worksheet

Set ATransWS = Worksheets("AL")
Set TransIDField = ATransWS.Range("A2", ATransWS.Range("A2").End(xlDown))
Set HTransWS = Worksheets("Onboarded")

' Find last row with data in column A on HTransWS sheet
lr = HTransWS.Cells(Rows.Count, "A").End(xlUp).Row

' Delete data from row 8 down
If lr >= 8 Then
HTransWS.Range("A8:A" & lr).EntireRow.Delete
End If

For Each TransIDCell In TransIDField

If TransIDCell.Interior.Color = RGB(198, 239, 206) Then

TransIDCell.Resize(1, 12).Copy Destination:= _
HTransWS.Range("A1").Offset(HTransWS.Rows.Count - 1, 0).End(xlUp).Offset(1, 0)

End If

Next TransIDCell

HTransWS.Columns.AutoFit

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,135
Messages
6,123,241
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top