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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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