Johnny Thunder
Well-known Member
- Joined
- Apr 9, 2010
- Messages
- 693
- Office Version
- 2016
- Platform
- MacOS
Hello All,
Hitting a wall with the last piece of the puzzle in my code. I am trying to include a variable (Column Letter) in a formula and I am getting an error in the code.
Any help fixing this is appreciated!
Here is my code:
Hitting a wall with the last piece of the puzzle in my code. I am trying to include a variable (Column Letter) in a formula and I am getting an error in the code.
Any help fixing this is appreciated!
Here is my code:
Code:
Option Explicit
'----------------------------------------------------------------------------------------
'--- Deletes unused formulas and applies print settings
'---------------------------------------------------------------------------------------
Sub CleanSheet()
Dim ws As Worksheet, ws2 As Worksheet
Dim wb1 As Workbook
Dim cell As Range, rng As Range, Header As Range
Dim Lastr As Long, LastR2 As Long
Dim CellCount As Double, FindString As String, rng2 As String, Result As String
'Application.ScreenUpdating = False
'Application.DisplayAlerts = False
'Declarations
Set ws = Sheets("Coversheet Trial Balance")
Set ws2 = Sheets("Drop In BW Raw Data")
LastR2 = ws2.Cells(Rows.Count, "B").End(xlUp).Row
FindString = "BALANCING (INTERCO)"
Set Header = ws2.Range("40:40")
'Defines how many rows to add
CellCount = Sheet4.Range("B42:B" & LastR2).Rows.Count
'Defines Current Year Result Column
For Each cell In Header
If cell.Value = "2019" And cell.Offset(1, 0).Value = "Result" Then
Result = Chr(cell.Column + 64)
Result = "='Drop In BW Raw Data'!" & Result & "43""" 'The String Result is the letter I in this instance
Else
End If
Next cell
'Populates the formulas on the Trial Balance Sheet
With ws
.Range("B7").EntireRow.Offset(1).Resize(CellCount - 1).Insert Shift:=xlDown
.Range("B8:E" & CellCount + 6).NumberFormat = "General"
.Range("B8:B" & CellCount + 6).Formula = "='Drop In BW Raw Data'!B43" 'FS Description
.Range("C8:D" & CellCount + 6).Formula = "='Drop In BW Raw Data'!E43" 'G/L Account
.Range("E7").Formula = Result
.Range("E7:E" & CellCount + 6).Formula = Result '<-------------------------------Line that errors out----------------
.Columns("E:E").NumberFormat = "_(#,##0.00_);_(#,##0.00);_(""-""??_);_(@_)"
'searches all of column B for string
Set rng = .Range("B:B").Find(What:=FindString, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
'Populates the total Formula
rng2 = rng.Address
Lastr = ws.Cells(Rows.Count, "B").End(xlUp).Row
.Range(rng2).Offset(0, 3).Formula = "=Sum(E7:E" & Lastr - 3 & ")"
End With
End Sub
Last edited: