I have a VBA function that returns a lot of information about the computer it's running on. I'd like to format the output neatly, with the titles left-justified and the results all tabbed on a neat line to the right.
Here's what I thought would work, and this is the solution on many sites I've consulted:
AllInfo = _
"Date/Time Opened:" & Space(33 - Len("Date/Time Opened:")) & Now() & vbCrLf & _
"Filename:" & Space(33 - Len("Filename:")) & Application.ActiveWorkbook.Name & vbCrLf & _
"Name in Cell B2:" & Space(33 - Len("Name in Cell B2:")) & Cells(2, 2).Value & vbCrLf & _
But the output, in both a message box and the body of an email, is jagged. The titles are all lined up on the left, of course, but the results aren't lined up nicely.
Can anyone see what I'm doing wrong?
This works:
Sub djfsdf()
allinfo = _
"Date/Time Opened:" & Space(33 - Len("Date/Time Opened:")) & Now() & vbCrLf & _
"Filename:" & Space(33 - Len("Filename:")) & Application.ActiveWorkbook.Name & vbCrLf & _
"Name in Cell B2:" & Space(33 - Len("Name in Cell B2:")) & Cells(2, 2).Value
Cells(3, 3) = allinfo
End Sub
with the proper font:
You are not including the string vars in the calculation for the number of spaces to add.
AllInfo = _
"Date/Time Opened:" & Space(33 - Len("Date/Time Opened:" & Now())) & Now() & vbLf & _
"Filename:" & Space(33 - Len("Filename:" & Application.ActiveWorkbook.Name)) & Application.ActiveWorkbook.Name & vbLf & _
"Name in Cell B2:" & Space(33 - Len("Name in Cell B2:" & Cells(2, 2).Value)) & Cells(2, 2).Value & vbLf & _
(etc)
I've also change your vbCRLF to just vbLF; that may or may not be correct.
Related
all:
I feel like this is something painfully obvious but I have been banging my head up against the wall for a while and can't seem to find a solution.
When entering the below formula into "M3"
=IFERROR(IF(ISBLANK(VLOOKUP(A3&"|"&E3,CHOOSE({1,2},OLD_PR!$A$3:$A$374&"|"&OLD_PR!$E$3:$E$374,OLD_PR!$O$3:$O$374),2,FALSE)),"",VLOOKUP(A3&"|"&E3,CHOOSE({1,2},OLD_PR!$A$3:$A$374&"|"&OLD_PR!$E$3:$E$374,OLD_PR!$O$3:$O$374),2,FALSE)),"")
with this code (ActiveCell = "M3")
ActiveCell.FormulaArray = _
"=IFERROR(IF(ISBLANK(VLOOKUP(A3&" & Chr(34) & "|" & Chr(34) & "&E3,CHOOSE({1,2},OLD_PR!$A$3:$A$" & OldPRLastRow & "&" & Chr(34) & "|" & Chr(34) & "&OLD_PR!$E$3:$E$" & OldPRLastRow & ",OLD_PR!$O$3:$O$" & OldPRLastRow & "),2,FALSE)),""""," _
& "VLOOKUP(A3&" & Chr(34) & "|" & Chr(34) & "&E3,CHOOSE({1,2},OLD_PR!$A$3:$A$" & OldPRLastRow & "&" & Chr(34) & "|" & Chr(34) & "&OLD_PR!$E$3:$E$" & OldPRLastRow & ",OLD_PR!$O$3:$O$" & OldPRLastRow & "),2,FALSE))," _
& Chr(34) & Chr(34) & ")"
I am getting the Run-time error in the title.
When I manually enter the formula, it works.
The formula is less than 255 characters without the replace work around, so I'm not making any mistakes there
I'm only entering the formula into a single cell
The above are the most common mistakes I found via google searches, but do not apply to me. I'm sure this is something silly.
Any insight would be greatly appreciated!
With reference to this try
ActiveCell.FormulaArray = _
"=IFERROR(IF(ISBLANK(VLOOKUP(D3&" & Chr(34) & "|" & Chr(34) & "&E3,""ChoosePart"",2,FALSE)),""""," _
& "VLOOKUP(D3&" & Chr(34) & "|" & Chr(34) & "&E3,""ChoosePart"",2,FALSE))," _
& Chr(34) & Chr(34) & ")"
ActiveCell.Replace """ChoosePart""", "CHOOSE({1,2},OLD_PR!$A$3:$A$" & OldPRLastRow & "&" & Chr(34) & "|" & Chr(34) & "&OLD_PR!$E$3:$E$" & OldPRLastRow & ",OLD_PR!$O$3:$O$" & OldPRLastRow & ")"
Since Column O contains text values, here's an alternative...
ActiveCell.FormulaArray = "=LOOKUP(REPT(""z"",255),CHOOSE({1,2},"""",INDEX(OLD_PR!$O$3:$O$" & OldPRLastRow & ",MATCH(A3&""|""&E3,OLD_PR!$A$3:$A$" & OldPRLastRow & "&""|""&OLD_PR!$E$3:$E$" & OldPRLastRow & ",0))))"
Can't seem to make the code work despite using spacing and operators around the code, can't pinpoint exact where the problem lies
Tried placing operators many times but still ended up with errors for example Expected: end of statement, as such this is the line of code that was highlighted:
userMsheet.Cells(lastrow + 1, "A").Formula = "=HYPERLINK(" & "#" &
ActiveSheet.Name & "!" & Cells(salesNo - 1, 3).Address & "," &
usernameComboBox.Value & ")"
Expected Result: The code should create a hyperlink in another worksheet when this button in the userform is pressed
Try following
userMsheet.Cells(lastrow + 1, "A").Formula = "=HYPERLINK(" & _
ActiveSheet.Name & "!" & Cells(salesNo - 1, 3).Address & "," & _
Chr(34) & usernameComboBox.Value & Chr(34) & ")"
SubAddress Issue not feat. #
I don't think that what you two are trying to do is even possible. Therefore I would suggest you add the hyperlink directly into the cell i.e. if you want it to point to something.
userMsheet.Hyperlinks.Add _
anchor:=userMsheet.Cells(lastrow + 1, "A"), _
Address:="", _
SubAddress:="'" & ActiveSheet.Name & "'!" _
& Cells(salesNo - 1, 3).Address, _
TextToDisplay:=usernameComboBox.Value
EDIT: Here is what I'm using:
Sub whatever()
ActiveSheet.Hyperlinks.Add _
anchor:=Range("A6"), _
Address:="", _
SubAddress:="'" & ActiveSheet.Name & "'!" & Cells(6, 3).Address, _
TextToDisplay:="Whatever"
End Sub
Your Code:
With userMsheet
lastrow = .Range("A" & Rows.Count).End(xlUp).Row
.Hyperlinks.Add _
anchor:=.Cells(lastrow + 1, "A"), _
Address:="", _
SubAddress:="'" & ActiveSheet.Name & "'!" _
& Cells(salesNo - 1, 3).Address, _
TextToDisplay:=usernameComboBox.Value
End With
I'm pretty new to VBA and I've been trying to use a macro to paste a few formulas in cells. Problem is, everytime I do so, I get a 1004 error on the very first formula.
I strongly suspect it's because of quotes but I can't for the life of me understand what goes wrong. Would you guys have any idea ?
Sub PREF()
Dim lastrow As Long
lastrow = Range("'Extract WIN'!A" & Rows.Count).End(xlUp).Row
Dim P2 As Worksheet
Set P2 = Sheets("PRO")
Select Case MsgBox("Do you want to proceed with" & P2.[C2].Value & " ?", vbYesNo, "as datepref")
Case vbYes
Sheets("Extract WIN").Select
Range("W2" & ":" & "W" & lastrow).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10];7;2);""/"";MID(RC[-10];5;2);""/"";MID(RC[-10];1;4)));TEXT(,))"
Range("Y2" & ":" & "Y" & lastrow).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10];7;2);""/"";MID(RC[-10];5;2);""/"";MID(RC[-10];1;4)));TEXT(,))"
Range("AA2" & ":" & "AA" & lastrow).FormulaR1C1 = "=IFERROR(IF(AND(Provision!R2C3-RC[-4]<366;RC[-18]>0);RC[-18];0);TEXT(,))"
Range("AB2" & ":" & "AB" & lastrow).FormulaR1C1 = "=IFERROR(RC[-1]*RC[-18];TEXT(,))"
Range("AC2" & ":" & "AC" & lastrow).FormulaR1C1 = "=IF(AND(RC[-2]=0;RC[-20]>0;RC[-4]>Provision!R2C6;ISNA(VLOOKUP(RIGHT(TEXT(RC[-25];""000#####"");4);Provision!R7C17:R101C18;1;FAUX))=FAUX);1;0)"
Range("AD2" & ":" & "AD" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-20]"
Range("AE2" & ":" & "AE" & lastrow).FormulaR1C1 = "=IFERROR(RC[-22]-RC[-4]-RC[-2];TEXT(,))"
Range("AF2" & ":" & "AF" & lastrow).FormulaR1C1 = "=IF(AND(RC[-20]>0;RC[-1]>0);ROUND(MIN(RC[-20]*12;RC[-1]);0);0)"
Range("AG2" & ":" & "AG" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-23]"
Range("AH2" & ":" & "AH" & lastrow).FormulaR1C1 = "=RC[-2]-RC[-18]"
Range("AI2" & ":" & "AI" & lastrow).FormulaR1C1 = "=IFERROR(RC[-4]-RC[-3];TEXT(,))"
Range("AJ2" & ":" & "AJ" & lastrow).FormulaR1C1 = "=IF(RC[-24]>0;ROUND(MIN(RC[-24]*12;RC[-1]);0);0)"
Range("AK2" & ":" & "AK" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-27]"
Range("AL2" & ":" & "AL" & lastrow).FormulaR1C1 = "=RC[-2]-RC[-21]"
Range("AM2" & ":" & "AM" & lastrow).FormulaR1C1 = "=IFERROR(RC[-4]-RC[-3];TEXT(,))"
Range("AN2" & ":" & "AN" & lastrow).FormulaR1C1 = "=IF(AND(RC[-16]>Provision!R2C7;RC[-28]>=0);RC[-1];0)"
Range("AO2" & ":" & "AO" & lastrow).FormulaR1C1 = "=IFERROR(RC[-1]*RC[-31];TEXT(,))"
Range("AP2" & ":" & "AP" & lastrow).FormulaR1C1 = "=IF(RC[-18]=TEXT(,);0;IF(AND(X2<Provision!R2C7;RC[-3]>0);RC[-3];0))"
Range("AQ2" & ":" & "AQ" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-33]"
Range("AR2" & ":" & "AR" & lastrow).FormulaR1C1 = "=IF(RC[-20]="""";RC[-5];0)"
Range("AS2" & ":" & "AS" & lastrow).FormulaR1C1 = "=IFERROR(RC[-1]*RC[-35];TEXT(,))"
Range("AT2" & ":" & "AT" & lastrow).FormulaR1C1 = "=IFERROR(RC[-6]+RC[-4]+RC[-2];TEXT(,))"
Range("AU2" & ":" & "AU" & lastrow).FormulaR1C1 = "=IFERROR(RC[-6]+RC[-4]+RC[-2];TEXT(,))"
Range("AV2" & ":" & "AV" & lastrow).FormulaR1C1 = "=IFERROR(RC[-2]-RC[-30];TEXT(,))"
Range("AX2" & ":" & "AX" & lastrow).FormulaR1C1 = "=IFERROR(RC[-13]*0,5;TEXT(,))"
Range("AY2" & ":" & "AY" & lastrow).FormulaR1C1 = "=IFERROR(RC[-4]*0,9;TEXT(,))"
Range("AZ2" & ":" & "AZ" & lastrow).FormulaR1C1 = "=IFERROR(RC[-2]+RC[-1];TEXT(,))"
Range("BA2" & ":" & "BA" & lastrow).FormulaR1C1 = "=IFERROR(RANK(RC[-1];RC:RC;0);TEXT(,))"
Columns("AA:AZ").NumberFormat = "#,##0"
Columns("W:BA").EntireColumn.AutoFit
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
Range("AI2").Select
Sheets("PRO").Select
Case vbNo
P2.[C2].Select
End Select
End Sub
I'm sorry for the wall of code but I have no idea where I did something wrong, and was advised to give you guys full context
You need to replace the ";" with regular ",".
For example, take this line of code:
Range("W2:" & "W" & LastRow).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10];7;2);""/"";MID(RC[-10];5;2);""/"";MID(RC[-10];1;4)));TEXT(,))"
And change it to this:
Range(Cells(2, 1), Cells(2, LastRow)).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10],7,2),""/"",MID(RC[-10],5,2),""/"",MID(RC[-10],1,4))),TEXT(,))"
Also, keep in mind I made a few changes to this line. I changed it so that it is using Cells instead of range. I find Cells are easier to work with and provide better readability because we don't have to join any strings.
Either way is fine, it just boils down to preference.
The cells which the code is pointing (b2, b3 ..) are already showing up 2 decimal houses, but the msg box shows many . How can I fix it? I had an attempt with & _
Format(dTotalArea, "0.00"), but didnt work.
Many thanks in advance.
MsgBox "First Response Time (hours)= " & _
Worksheets("Results").Range("B2").Value & _
vbCrLf & "Investigation Time (hours)= " & _
Worksheets("Results").Range("B3").Value & _
vbCrLf & "Nr of Incidents Resolved= " & _
Worksheets("Results").Range("B4").Value & _
vbCrLf & "Resolved Same Day (%)= " & _
Worksheets("Results").Range("B5").Value & _
Format(dTotalArea, "0.00"), , "title_of_msgbox"
Try this
MsgBox "First Response Time (hours)= " & _
Worksheets("Sheet1").Range("B2").Value & _
vbCrLf & "Investigation Time (hours)= " & _
Format(Worksheets("Sheet1").Range("B3").Value, "0.00") & _
vbCrLf & "Nr of Incidents Resolved= " & _
Format(Worksheets("Sheet1").Range("B4").Value, "0.00") & _
vbCrLf & "Resolved Same Day (%)= " & _
Format(Worksheets("Sheet1").Range("B5").Value, "0.00") _
, , "title_of_msgbox"
I need to add a string to an email in richtext format. I cannot format the text out of a textbox by selecting it as I usually do. Can I just modify the string? And how could I do that?
Currently the text was formatted in HTML but that isn't an option anymore. Looks something like this
string = vbCrLf & vbCrLf & "<B><FONT face=Arial color=#365f91 size=2>" & _
strName & "</FONT>" & _
"<FONT face=Arial size=2><BR>" & _
"<I>" & strPosition & "</I>" & _
"</FONT></B><BR/><B><FONT face=Arial size=2>" & _
strAddress1 & "</FONT></B><BR/><FONT face=Arial size=1>" & _
strAddress2 & "<BR>" & strCity & ", " & _
strProvince & ", " & strPostalCode & _
"<BR>" & strCountry & "<BR>Office: " & strPhone & _
"<BR>" & strEmail & "<BR>www.website.com<BR>" & _
" " & _
"<I><FONT face=Arial size=2>" & strImageCaption & "</FONT>" & _
"<BR/><BR/><BR/></I><FONT face=Arial size=1>" & strDisclaimer & _
"</FONT></P>"
I can't find any resources online about formatting rich text that isn't currently in a rich text box so I think maybe it isn't possible.