I'm trying to trim the headings of a table with VBA code because some of them have spaces in front that varies every month, which makes it difficult for coding.
When I break down the code and run it step by step it works fine but when I run the whole macro it removes some of my headings and replace them with info from a different sheet row or just "column 1", "column 2", etc.
I believe I'm missing some code reference when it calls the (" & .Address & ") selection?
It's replacing the headings from a sheet where the cell is active. (If it was the last cell to click on before running the macro).
I've tried just using the Trim function, but because it's an array for the range, it doesn't work, and someone suggested to use the "evaluate" function.
I've tried using the trim function as a WorksheetFunction as well but it gave me an error "Run-time error 13" Type-mismatch". Which was on the following code:
With wsDispoData.ListObjects("Table_DispoData").HeaderRowRange.EntireRow
.Value = WorksheetFunction.Trim(.Value)
End With
This is the current code I'm using that replaces wrongly.
Trim headings
With wsDispoData.ListObjects("Table_DispoData").HeaderRowRange.EntireRow
.Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
End With
Expected results should be for example:
Current headings: " SOH" and " Compo"
Trimmed: "SOH" and "Compo"
I would just enumerate through the header row and check each value
Dim Cell As Range
For Each Cell In wsDispoData.ListObjects("Table_DispoData").HeaderRowRange
Cell.value = WorksheetFunction.Trim(Cell.value)
Next Cell
Related
I am attempting to place the following formula into a cell through VBA:
=IF('Other Sheet'!D2="", 'Other Sheet'!D1, 'Other Sheet'!D2)
I have replaced the row number with variables and am attempting to concatenate them together as the formula is repeated as part of a loop.
ActiveCell.Formula = "=IF('Other Sheet'!D" & Row2 "="""", 'Other Sheet'!D" & Row1 ", 'Other Sheet'!D" & Row2 ")"
The formula works without variables when pasted into a cell or when in a line as vba however when I attempt to split it up, I get either a syntax error or an expected end of statement error around the equal sign.
I am very new to VBA and only have about 2 weeks experience with it.
I have tried troubleshooting by breaking apart the if statement and changing the location of double quotes and adding the .Formula suffix but nothing seems to be working.
Ideally after concatenation the code would repeat for each loop increasing by 5
ie.
first pass =IF('Other Sheet'!D2="", 'Other Sheet'!D1, 'Other Sheet'!D2)
second pass =IF('Other Sheet'!D7="", 'Other Sheet'!D6, 'Other Sheet'!D7)
Almost there - missing a couple of &
ActiveCell.Formula = _
"=IF('Other Sheet'!D" & Row2 & "="""", 'Other Sheet'!D" & Row1 & ", 'Other Sheet'!D" & Row2 ")"
Kudos to Tim Williams. I just wanted to explain how I convert formulas into code.
Create a working formula in the ActiveCell
Print the Actual formula in the Immediate Window: ?ActiveCell.Formula
Print the test formula code in the Immediate Window: '?Replace(ActiveCell.Formula,Chr(34),String(2,34))'
Type ?" and the output of step 2.
Start building my formula
Shortcut keys:
Delete Line: Ctrl+y
Insert Line: Ctrl+Enter
I have a button that creates a table, (report generator)
in this table i link to various pages with the same syntax.
Worksheets("Engine").Range("E" & EngineStatus).Formula = "=" & newSheetName & "!I3"
It is working, and it is running on a loop.
However, when
I am linking the value from each sheet, in the same cell on each sheet.
but of course only for selected sheets.
The problem occur on the report, as seen on the screenshot.
Sheetname in this case is ACDTCM0137 and cell i3 is what i put in my code..
The output is it counts i as a increased number which it should not.
And it overwrites ALL rows in this column with it's LAST value..
So the last sheet might be called BDMETHR0148 and same cell..
But the last one in, is the one it shows for ALL rows.
How do i ensure that for each row it keeps the formatting from the cove above?
Meaning it should always bee by this syntax
You should read about relative and absolute reference in Excel.
Instead:
Worksheets("Engine").Range("E" & EngineStatus).Formula = _
"=" & newSheetName & "!I3"
Use:
Worksheets("Engine").Range("E" & EngineStatus).Formula = _
"=" & newSheetName & "!$I$3" ' dollar sign $ before column and row locks it
You wrote that you have button to create report, so maybe the code below will be even better (not recalculating until report generated again - values only, not formula):
' all vars before loop "dimmed" only once
(...)
Dim rngEngine As Range
Dim rngStatus As Range
' And in your loop
EngineStatus = ...
newSheetName = ...
Set rngEngine = Worksheets("Engine").Range("E" & EngineStatus)
Set rngStatus = Worksheets(newSheetName).Range("I3")
rngEngine.Value = rngStatus.Value
I have a working formula in excel and I am trying to put it into a macro to add the formula to column A. However I get a Expected end of statement error message at "Payer ID is not set up".
Worksheets("1099-Misc_Form_Template").Range("B2:B & LastRow").Formula = "=IF(COUNTIF('payorTab'! A:A, B2)>0, "", "PayerID is not set up")"
End Sub
For the macro to insert the formula into the column A.
try this
Worksheets("1099-Misc_Form_Template").Range("B2:B" & LastRow).Formula = "=IF(COUNTIF('payorTab'!A:A,B2)>0, """", ""PayerID is not set up"")"
I'm not sure if this is even possible in Excel but this is what I need to do:
I have a column with a list of hotels and then another column which needs to pull data from each individual hotel's excel file. For example, cell A2 will have the name "Paris" for the hotel and then cell B2 will have the link:
='G:\Hotels\Paris\Paris - Monthly\[Paris_summary_2018.xlsm]Feb'!$CD$89
I have lots of hotels I need to do this for in different sheets. So I need the link to dynamically refer to whatever hotel is in column A and the title of the sheet, for example could I do something like this?
=''G:\Hotels\A2\A2 - Monthly\[A2_summary_2018.xlsm]Feb'!$CD$89
where A2 has the string "Paris". Also is there a way to dynamically refer to "Feb" depending on what sheet I am in the month will be the title
I am also open to using VBA for this.
As long as you don't mind using VBA, you can easily generate the links with something like this:
Sub generate_hotel_links()
Dim r As Range, c As Range
Dim s As String
' This is the range which all the hotel-locations are in
Set r = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10")
On Error Resume Next
For Each c In r
' Generate the formula based on the current cell we are in
s = "=" & Chr(39) & "G:\Hotels\" & CStr(c) & "\" & CStr(c) & " - Monthly\[" & CStr(c) & "_summary_2018.xlsm]Feb" & Chr(39) & "!$CD$89"
' ...and put it in the neighbouring cell
c.Offset(0, 1).Formula = s
Next c
On Error Goto 0
End Sub
On Error Resume Next will make the macro continue no matter what error pops up - the ideal case would be some more robust error handling, or a check for if the workbook / sheet actually exists before attempting to write the formula, but I'll leave it to you to attempt to write this if you feel the need improve the macro.
If you want to just use generic Excel formulas, I'd advice having a look at the question and answers I posted in the comments to your question.
I am not sure if you can use HYPERLINK.
Like this:
=HYPERLINK("G:\Hotels\"&A2&"\"&A2&" - Monthly\["&A2&"_summary_2018.xlsm]Feb!"&$CD$89)
I have created a code that adds a new line to my spreadsheet with the information that users enter into a user-form. The problem I'm having is that I have formulas in two of the columns that I need to be carried into the new rows. I have tried several different ways to do this but keep receiving an error message. The formula I need carries is
=IFERROR(INDEX(Equipment!$E$3:$E$1000,MATCH($B5&"-"&$A5,INDEX(Equipment!$B$3:$B$1000&"-"&Equipment!$A$3:$A$1000,0),0)),"")
The values that change line to line are the B and A cells after the MATCH command (in this case $B5 and $A5). Can anyone help me figure out how to convert this to VBA syntax so that I can add it to my code? Thanks in advance!
I Converted this formula, and tested it in a loop.
The formula is copied to Column D from rows 11 to 20, where $B5 and $A5 change according to whatever row they are (I hope I understand what you meant).
Sub ConvertFormula()
' modify to where (worksheet) you want to copy this formula
Set eqp_sht = ActiveWorkbook.Worksheets("Equipment")
' modify this according to whatever rows and column you want this formula copied
For i = 11 To 20
eqp_sht.Cells(i, 4).Formula = "=IFERROR(INDEX(Equipment!$E$3:$E$1000,MATCH($B" & i & "&-$A" & i & ",INDEX(Equipment!$B$3:$B$1000&-Equipment!$A$3:$A$1000,0),0))," & Chr(34) & Chr(34) & ")"
Next
End Sub