Using user range input for a formula - excel

I am trying to create a VBA code that will pull the info to the Left (eventually the right) of a Hyphen based on a range and a cell given by the user.
EX:
Result A & Result B are what I am trying to get too.
I've tested everything in my code until this part and it all works. The entire thing works if I physically type in a cell address (i.e $D2 - I will need the column absolute, but the row relative so that it moves with the range selection). I just can't get it to work with the user input of the "Starting" variable. I need it to be user input because this code will be used on sheets set up completely different than this one. There is a good chance I am missing something obvious but I'm not seeing it #_#. Any suggestions?
**Sorry in advance for the long lines of code
Private Sub Seperate_XtoY_Click()
Dim iCol As Long
Dim iCount As Long
Dim i As Long
Dim Smaller As Range
Dim Bigger As Range
Dim Starting As Range
'Get number of columns that you want to insert with a user input box
iCount = InputBox(Prompt:="How many columns you want to add?")
'Get column NUMBER where you want to insert the new column
iCol = InputBox _
(Prompt:= _
"BEFORE which column do you want to add the new column(s)? (Enter the column number i.e A=1, B=2, C=3, etc)")
'loop to insert new column(s)
For i = 1 To iCount
Columns(iCol).EntireColumn.Insert
Next i
'Makes range variable "Starting" equal to the user input of a range (in this case just 1 cell)
Set Starting = Application.InputBox("Select the FIRST cell of the Original Range of #'s", "Obtain Range Object", Type:=8)
'Makes range variable "Smaller" equal to the user input of a range (where the info will actually populate)
Set Smaller = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)
Smaller.Formula = "=IF(ISNUMBER(SEARCH(""½"", & Starting.Address(0, ""$"") &)),""0.5"",IF(ISNUMBER(SEARCH(""¼"",& Starting.Address(0, ""$"") &)),""0.25"",IF(ISNUMBER(SEARCH(""¾"",& Starting.Address(0, ""$"") &)),""0.75"",LEFT( &Starting.Address(0, ""$"")&, FIND(""–"",& Starting.Address(0, ""$"")&)-1))))"
End Sub

It turns out I had the right idea based on my last comment. I did need remove the variable completely out of the quotes (and then restart them), double check the placing of where I put those quotes, and use a different version of the .Address function to make only my column an absolute reference. All the other lines of code from above were all good, it was just the final line that needed to changed. Thank you #BigBen for giving me a push in the right direction. Looking at the program with fresh eyes also helped lol.
Smaller.Formula = "=IF(ISNUMBER(SEARCH(""½""," & Starting.Address(RowAbsolute:=False) & " )),""0.5"",IF(ISNUMBER(SEARCH(""¼"", " & Starting.Address(RowAbsolute:=False) & ")),""0.25"",IF(ISNUMBER(SEARCH(""¾"", " & Starting.Address(RowAbsolute:=False) & " )),""0.75"",LEFT( " & Starting.Address(RowAbsolute:=False) & " , FIND(""–"", " & Starting.Address(RowAbsolute:=False) & ")-1))))"
I also got the Right side function working too if anyone is interested:
Bigger.Formula = "=IF(ISNUMBER(SEARCH(""– ½""," & Starting.Address(RowAbsolute:=False) & ")),""0.5"",IF(ISNUMBER(SEARCH(""– ¼""," & Starting.Address(RowAbsolute:=False) & ")),""0.25"",IF(ISNUMBER(SEARCH(""– ¾""," & Starting.Address(RowAbsolute:=False) & ")),""0.75"",RIGHT(" & Starting.Address(RowAbsolute:=False) & ",LEN(" & Starting.Address(RowAbsolute:=False) & ")-FIND(""– ""," & Starting.Address(RowAbsolute:=False) & ")-1))))"
PLEASE NOTE for anyone who may want to use a variation of my code I used a slightly bigger hyphen than the typical hyphen ("-" vs "–")

Related

Offset address to be used in dynamic formula

I am trying to identify a cell in excel using VBA which is going to be used to add a dynamic formula.
This is what I have done so far:
STCELL = Sheets("ADMIN").Range("h" & Rows.Count).End(xlUp).Address(False, False)
The above line gives me cell "H16"
Now I need to offset this address. I thought about this:
STCELL1 = Range(" & STCELL & ").Offset(1, 0)
but unfortunately is not working. The results of the STCELL (start cell) should be used in the next line:
Sheets("admin").Range("H" & Rows.Count).End(xlUp).Offset(1, 0).Formula = "=INDEX(Mapping!$D:$F,MATCH(" & STCELL & ",Mapping!$A:$A,0),1)"
the address of STCELL is properly read by VBA in the last line, but I need to offset it since the location of the cell where the formula is going to be applied will keep changing.
So, question is, how can I offset the address I have identified with my first line of code?
Thank you for all your help.
Since it is the Range.Offset property, focus on offsetting a Range, not the address of a Range:
Dim rng As Range
Set rng = Sheets("ADMIN").Range("h" & Rows.Count).End(xlUp)
STCELL = rng.Address(False, False)
STCELL1 = rng.Offset(1).Address(False, False)

Copying a string, which is all numerical digits

Am going crazy trying to just copy and paste in Excel.
I have a worksheet that I am exporting, from AutoCAD if it matters, and then trying to copy and paste a section of it onto another sheet. One of the columns has a four-digit number, starting with 0, that I would like to keep as a string. (I think AutoCAD is exporting it as a string, and expects it back as a string for importing, which explains why I do not want any workaround where the string TYPE is lost)
When I do the copy/paste "manually" with CTRL+C and CTRL+V, the columns paste fine (e.g., "0101" pastes as "0101" although the top left corner of the cells are greened with a message that says "the number in the cell is formatted as text". I'm thinking: no, it's a string, just leave it alone, please! And it does leave it alone, there.
However, my VBA script to do the same seems to lose the TYPE of the value when pasting (e.g., "0101" becomes 101, and there are no green corners with comments). I have verified this with the manual TYPE function under the Formulas bar.
Here is my script:
ThisWorkbook.Worksheets("Sheet1").Range("B" & numTemplateHeaderRows + 1 & ":Y" & numImportRows + 1).Value = _
wsImport.Range("B" & numTemplateHeaderRows + 1 & ":Y" & numImportRows + 1).Value
I've even tried to Dim an array, put the values in there, and then iterate through the problematic column with Cstr. Still, when I set the values on the receiving sheet, those Strings become numbers.
Does anyone know what might be causing Excel to do this conversion? Can I turn it off, please?
If you want to copy-paste a cell or range, use PasteSpecial:
Sub copyRange(wsImport As Worksheet, numTemplateHeaderRows As Integer, numImportRows As Integer)
Dim org As Range, dest As Range
Set org = wsImport.Range("B" & numTemplateHeaderRows + 1 & ":Y" & numImportRows + 1)
Set dest = ThisWorkbook.Worksheets("Sheet1").Range("B" & numTemplateHeaderRows + 1 & ":Y" & numImportRows + 1)
org.Copy
dest.PasteSpecial xlPasteAll
End Sub

VBA formula removing everything after second space

I was trying to copy from column D to column E first two words of each row but still can not find where the error is....
Range("E1:E" & lLastRow).Formula = "=LEFT(D1,FIND("" "",D1,FIND("" "",D1)+1)-1)"
Another option, instead of using a Formula, you can use the Split function.
Code
Dim i As Long, LastRow As Long
Dim WordsArr As Variant
' loop through rows
For i = 1 To LastRow
WordsArr = Split(Range("D" & i).Value, " ") ' use Split and space to read cell words to array
If UBound(WordsArr) >= 1 Then ' make sure the cell contents is at least 2 words
Range("E" & i).Value = WordsArr(0) & " " & WordsArr(1) ' insert only the first 2 words
Else ' in case there are less than 2 words
' do someting....
End If
Next i
End Sub
Try this instead ...
Range("E1:E" & lLastRow).FormulaR1C1 = "=LEFT(RC[-1],FIND("" "",RC[-1],FIND("" "",RC[-1])+1)-1)"
I find using R1C1 better for those sort of operations, especially given you want your references to be dynamic, not absolute.
Alternatively, add the formula you had normally and simply fill down.

Find crashes when nothing is found

I have a sheet with a list of course names called Matrix. In another sheet named Courses Date I will have the same courses with the date they were taken.
Example:
The course named Safety Driving will be in Matrix on row 1. In Courses Date there is data from E1:BF1 with the same name. If courses need a refreshment there will be another column named exactly the same name of the course + Refresher (Safety Driving Refresher).
What I am trying to do is to find if a course has a refresher or not. My code returns Run-time error '91': Object variable or With block variable not set if nothing is found.
This is my code:
RefresherColNumber = Range("'Courses Date'!E1:BF1").Find(Range("'Matrix'!" & courseColLetter & "1").Value & " Refresher").Column
Add a check
Dim refreshRange As Range
Set refreshRange = Range("'Courses Date'!E1:BF1").Find(Range("'Matrix'!" & courseColLetter & "1").Value & " Refresher")
If Not refreshRange Is Nothing Then RefresherColNumber = refreshRange.Column
You could use Application.Match to test, with IsError, if value found in row and add 4 to where found to get the column. You should qualify your ranges with the parent worksheet object as well to avoid bugs with implicit activesheet referencing.
Dim matchValue As Variant
matchValue = Application.Match(Range("'Matrix'!" & courseColLetter & "1").Value & " Refresher", Range("'Courses Date'!E1:BF1"), 0)
If Not IsError(matchValue) Then RefresherColNumber = matchValue + 4

Generate serial numbers and barcodes as per logic defined based on the count mention in certain cell

I need to generate lot numbers for my products and since my software doesn't auto generate them, I have defined a logic in Excel for the same. It is quite basic and combines data in 2 or more cells to create a unique code.
To be more specific, I shall introduce our operations to you. We have a few collection centers for our products and we require them to apply lot number labels onto their bags of coffee before transferring it to our main warehouse. I have created an excel sheet in which I enter the current date in one cell and select the name of the center in another which then generates a lot number for the same. For example, cell A1 has today's date and A2 has center 'MBR' selected. The formula in cell C2 =IF(A2="MBR","MB:"&TEXT(A1,"YYYYMMDD")&"-001",IF(A2="MAY","AY:"&TEXT(A1,"YYYYMMDD")&"-001",IF(A2="MZM","MM:"&TEXT(A1,"YYYYMMDD")&"-001",""))) shall give me a result as MB:20171010-001
Now comes the tricky part. I want to mention in cell A3 the number of lots to be generated. For example, if I say I want 10 labels, then the formula should give me 10 labels from "-001" to "-010." If possible, it could start with the number which I would define in possibly cell A4 and then give me the sequence as required.
Lastly, I use this info to generate barcodes using an add-on in excel which I downloaded from the internet which converts the text into barcodes and then I can print them. I have tried barcode fonts but they don't work at all. If you have another alternative to this where I could generate sequential lot numbers based on the logic defined and create barcodes for the same, please share it with me. If not, please give me a formula which will work with excel.
Enter your data in columns A - C, run the bit of sample code below, result is in column D. You can modify the code to suit your needs but here is a start for you:
Public Sub RunTags()
Dim oWS As Worksheet
Dim lRow As Long
Dim lNextTagRow As Long
Set oWS = Worksheets("Sheet1")
lRow = 2
lNextTagRow = 2
Do Until oWS.Range("A" & lRow) = ""
Select Case oWS.Range("A" & lRow)
Case "MBR"
PrintTags oWS, lNextTagRow, "MB:", oWS.Range("B" & lRow), oWS.Range("C" & lRow)
Case "MAY"
PrintTags oWS, lNextTagRow, "MA:", oWS.Range("B" & lRow), oWS.Range("C" & lRow)
Case "MZM"
PrintTags oWS, lNextTagRow, "MZ:", oWS.Range("B" & lRow), oWS.Range("C" & lRow)
End Select
lRow = lRow + 1
Loop
End Sub
.
Private Sub PrintTags(ByRef oWS As Worksheet, ByRef lTagRowStart As Long, sPrefix As String, lQty As Long, lStart As Long)
Dim x As Long
Dim dtNow As Date
dtNow = Now()
For x = lStart To lStart + lQty - 1
oWS.Range("D" & lTagRowStart) = sPrefix & Format(dtNow, "YYYYMMDD") & "-" & Format(x, "000")
lTagRowStart = lTagRowStart + 1
Next x
End Sub

Resources