VBA Coding Help needed. Trying to build a table, based on a list from a different tab.

jpj31

New Member
Joined
Apr 26, 2022
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to create a table to show costs by floor. I've got toggles to turn them off or on, on a setup tab (I'll add pictures). The number of floors will vary, so I need that to update. I've got another tab that has the actual calculation and a third where the table is being built. Currently it works with the very first (last) floor but doesn't continue up the list. Also, I've defined the worksheets in my code but when I try to reference them, it wasn't working (Ex. Ws1.Range("P2:T2").Select wouldn't work, so I had to use Sheets("PM COST CODES").Select Range("P2:T2").Select) If I'm doing something wrong, I'd like to understand so I can fix it.

My current coding is below:
Sub CCBFB()



Dim nr As Long

Dim ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet

Dim lastrow As Long

Dim lastcolumn As Long

Dim totalrow As Long

Dim lc As Range

Dim i As Long



Set ws = Worksheets("Setup")

Set ws1 = Worksheets("PM COST CODES")

Set ws2 = Worksheets("Cost Codes by Floor Build")



Application.ScreenUpdating = False



' Initialize to start at bottom of column K

nr = Rows.Count



' last row in column K

lastrow = ws.Cells(Rows.Count, 11).End(xlUp).Row

' Loop through rows

Do

' Find next row up with data

nr = Cells(nr, "K").End(xlUp).Row

' Exit if at row 2

If nr = 2 Then Exit Do

' Turn Off the current level

With Cells(nr, "N").Select

ActiveCell.FormulaR1C1 = "Off"



Cells(nr, "K").Select

Selection.Copy



Sheets("PM COST CODES").Select

Range("P2:T2").Select

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

:=False, Transpose:=False



ws1.Columns("P:T").Select

Selection.Copy



' last column in Cost Codes by Floor Build



lastcolumn = ws2.Cells(5, Columns.Count).End(xlToLeft).Column

Sheets("Cost Codes by Floor Build").Select

Columns(lastcolumn + 1).Select

ActiveSheet.Paste

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

:=False, Transpose:=False



End With



Ws.Cells(nr, "N").Select

ActiveCell.FormulaR1C1 = "Off"



Loop
Setup Tab.PNG
Floor Calculation.PNG
Table.PNG
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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