Quick Access button for VBA

spb

Board Regular
Joined
Sep 29, 2006
Messages
51
I have a macro that i have successfully run several times in the past. I have it linked to a button on my quick access toolbar.
I have a dataset on "sheet1" with row 1 being the column headers and column 1 being a unique identifier. The data fields are numbers and the macro simply creates a new row on sheet 2 for any row/column intersection that contains a number. So instead of having 50 columns I have 3, one with the unique identifier, one with the column heading and the last with the quantities.
I create a blank "sheet2" and attempt to run the macro and nothing happens. I've had this problem in the past and gotten it to work but i use it so infrequently I can't figure out the problem.
Any idea what i might be doing wrong? Thanks.

Sub flattenWorkSheet()
Application.ScreenUpdating = False
RowCount = 2
PrintRow = 1
With ActiveWorkbook
Do Until ActiveWorkbook.Sheets(1).Cells(Rows.Count, 1) = ""
ColumnCount = 2

Do Until ActiveWorkbook.Sheets(1).Cells(1, ColumnCount) = ""

If Sheets(1).Cells(RowCount, ColumnCount) <> "" Then
Sheets(2).Cells(PrintRow, 1) = Sheets(1).Cells(RowCount, 1)
Sheets(2).Cells(PrintRow, 2) = Sheets(1).Cells(1, ColumnCount)
Sheets(2).Cells(PrintRow, 3) = Sheets(1).Cells(RowCount, ColumnCount)
PrintRow = PrintRow + 1

End If

ColumnCount = ColumnCount + 1
Loop
RowCount = RowCount + 1
Loop
End With
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Never mind. I removed "ActiveWorkbook" references and it ran fine.
Thank you.

Sub flattenWorkSheet()
Application.ScreenUpdating = False
RowCount = 2
PrintRow = 1
Do Until Sheet1.Cells(RowCount, 1) = ""
ColumnCount = 2

Do Until Sheet1.Cells(1, ColumnCount) = ""

If Sheet1.Cells(RowCount, ColumnCount) <> "" Then
Sheet2.Cells(PrintRow, 1) = Sheet1.Cells(RowCount, 1)
Sheet2.Cells(PrintRow, 2) = Sheet1.Cells(1, ColumnCount)
Sheet2.Cells(PrintRow, 3) = Sheet1.Cells(RowCount, ColumnCount)
PrintRow = PrintRow + 1

End If

ColumnCount = ColumnCount + 1
Loop

RowCount = RowCount + 1
Loop
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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