I have a code as below. there are 2 workbooks I have used. one of them is taking the data from system and the other workbook is filled by users manually.
code is combining 2 columns on both workbooks and search for the data on 2nd workbook. if matchs, adding the data, which is in 1st workbook, to workbook 2.
unless there is another column need to be added this code. but there is an issue, 3rd column on workbook 2 can have typo mistakes or shorten versions of the words.
so, user need to decide which cell need to be filled.
for example ;
so below macro directly find and adding the data to 1st and 4th rows. but I know the data need to be added on only 4th row because user can understand tt means on 2nd workbook column 3 is Turk Telekom. so I need a msgbox addition to ask user column 3 is xx do you want to add, and also ask on 4th row column 3 is tt do you want to add.
findstring1 = wb.Sheets("Sheetname").Range("E4").Value
findstring2 = wb.Sheets("sheetname").Range("E5").Value
firstrow = 2
lastcell = s.Cells(2, 1).End(xlDown).Row
For i = firstrow To lastcell
If Left(s.Cells(i, 3), 3) = findstring1 And s.Cells(i, 4) = findstring2 Then
If s.Cells(i, 21) = "" Then
s.Cells(i, 21) = wb.Sheets("sheetname").Range("I4")
Else
s.Cells(i, 22) = s.Cells(i, 22).Value & " done on " & wb.Sheets("sheetname").Range("I4") & "."
End If
End If
Next i
firstrow = 2
lastcell = s.Cells(2, 1).End(xlDown).Row
For i = firstrow To lastcell
If Left(s.Cells(i, 3), 3) = findstring1 And s.Cells(i, 4) = findstring2 Then
Dim myReply As Integer
myReply = MsgBox("Matched code is as below. Would you like to Continue? [YES] Or Not [NO] ?" & " " & s.Cells(i, 6), _
vbYesNo + vbQuestion, "code Check")
Select Case myReply
Case Is = vbYes
Application.Cursor = xlWait
If s.Cells(i, 21) = "" Then
s.Cells(i, 21) = wb.Sheets("sheetname").Range("I4")
Else
s.Cells(i, 22) = s.Cells(i, 22).Value & " bla bla bla " & wb.Sheets("sheetname").Range("I4") & "."
End If
Case Is = vbNo
End Select
End If
Next i
worked for me
Related
I have written a simple VBA script (code below) that should inspect every cell in a certain column. Here I want to do some string manipulation ( i wante to search for "." in the string and then take the right side, but because I could not get it to work I always take the 4 digit as a start). I then copy the manipulated string into another cell and later back. The code works, but for some reason, it takes ages to run on only 35 cells!
I´m still a kook on VBA and wanted to get input what could be the reason for it and what I could improve to get a faster runtime. Is it because I take all strings froms 4 up to 50 ?
Sub EditStatus()
Application.DisplayAlerts = False
ActiveSheet.Name = "Backend"
myNum = Application.InputBox("Please enter the row number until which you would like to update the status column (only for new entries)")
For i = 2 To myNum
Cells(i, 20).Value = Mid(Cells(i, 10), 4, 50)
Cells(i, 10).Value = Cells(i, 20).Value
Next i
Range("T1:T200").Clear
Application.DisplayAlerts = True
End Sub
Thanks
No need for a loop. You can enter the formula in the entire range in 1 go and then convert them to values before putting the values back in Col J
Replace
For i = 2 To myNum
Cells(i, 20).Value = Mid(Cells(i, 10), 4, 50)
Cells(i, 10).Value = Cells(i, 20).Value
Next i
With
With Range("T2:T" & myNum)
.Formula = "=MID(J2, 4, 50)"
.Value = .Value
Range("J2:J" & myNum).Value = .Value
End With
Alternatively, you can directly perform the same action in Col J without the helper column T. For example you can do all that in 1 line as explained HERE as well
Simply replace
For i = 2 To myNum
Cells(i, 20).Value = Mid(Cells(i, 10), 4, 50)
Cells(i, 10).Value = Cells(i, 20).Value
Next i
Range("T1:T200").Clear
with
Range("J2:J" & myNum).Value = Evaluate("Index(MID(" & "J2:J" & myNum & ", 4, 50),)")
Replace Values In-Place
Adjust the values in the constants section.
This solution overwrites the data and doesn't use a helper column, but you can test it with one indicated near the end of the code.
Solve the renaming (Backend) part as needed.
The Code
Option Explicit
Sub EditStatus()
' Define constants.
Const sPrompt As String = "Please enter the row number until which you " _
& "would like to update the status column (only for new entries)"
Const sTitle As String = "Enter Number"
Const wsName As String = "Backend"
Const First As Long = 2
Const cCol As Long = 10 ' J
Const Delim As String = "."
' Define workbook.
Dim wb As Workbook: Set wb = ThisWorkbook ' Workbook containing this code.
' Require input.
Dim Last As Variant
Last = Application.InputBox( _
Prompt:=sPrompt, Title:=sTitle, Default:=First, Type:=1)
' Validate input.
If VarType(Last) = vbBoolean Then
MsgBox "You cancelled."
Exit Sub
End If
If Last < First Then
MsgBox "Enter a number greater than " & First - 1 & "."
Exit Sub
End If
If Int(Last) <> Last Then
MsgBox "Enter a WHOLE number greater than " & First - 1 & "."
Exit Sub
End If
' Define column range.
Dim rg As Range
Set rg = wb.Worksheets(wsName).Cells(First, cCol).Resize(Last - First + 1)
' Write values from column range to array.
Dim Data As Variant
If rg.Rows.Count > 1 Then
Data = rg.Value
Else
ReDim Data(1 To 1, 1 To 1): Data = rg.Value
End If
' Declare additional variables
Dim cValue As Variant ' Current Value
Dim i As Long ' Current Row (Array Row Counter)
Dim fPos As Long ' Current Delimiter Position
' Replace strings containing the delimiter, with the sub string
' to the right of it.
For i = 1 To UBound(Data)
cValue = Data(i, 1)
If Not IsError(cValue) Then
fPos = InStr(1, cValue, Delim)
If fPos > 0 Then
Data(i, 1) = Right(cValue, Len(cValue) - fPos)
End If
End If
Next i
' Maybe rather test with the following (writes to column 20 ("T")).
'rg.Offset(, 10).Value = Data
' Write values from array to column range.
rg.Value = Data
End Sub
I am looking for a way to shorten my code to input data from a form of 10 entries.
This is my userform with one RMA number (applies to all 10 PN), one customer name, 10 part numbers, and 10 serial numbers that go with each part number.
This is how I want data transferred to the worksheet.
The part number textboxes are named TB#.
The serial number textboxes are named SNTB#.
This is the code I have for the first entry. I was thinking of adding code to say "TB"&"i" and "SNTB"&"i", but I don't know where to place that statement or how to start it.
Private Sub EnterButton_Click()
'this assigns receiving data to first columns of log Sheet
If TB1.Value = "" Then
Else
Worksheets("RM Tracker").Activate
Dim lastrow
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastrow = lastrow + 1
Cells(lastrow, 1) = RMATB.Value
Cells(lastrow, 2) = CustCB.Value
Cells(lastrow, 3) = TB1.Value
Cells(lastrow, 4) = SNTB1.Value
Cells(lastrow, 5) = ReceiveTB.Value
ActiveCell.Offset(1, 0).Select
End If
ActiveWorkbook.Save
Call resetform
End Sub
Sub resetform()
RMATB.Value = ""
CustCB.Value = ""
TB1.Value = ""
SNTB1.Value = ""
ReceiveTB = ""
'sets focus on that first textbox again
RecForm.RMATB.SetFocus
End Sub
You can incorporate a for loop where "i" represents the row you are working with. When you are appending data you need to put that reference within the loop so the new row is recalculated.
Private Sub EnterButton_Click()
'this assigns receiving data to first columns of log Sheet
If TB1.Value = "" Then
Else
Worksheets("RM Tracker").Activate
dim i as long
For i = 1 To 10
Dim lastrow as long ' should put a data type with dim statements
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastrow = lastrow + 1
Cells(lastrow, 1) = Userform1.Controls("RMATB" & i).Value ' change userform name to fit your need
Cells(lastrow, 2) = Userform1.Controls("CustCB" & i).Value
Cells(lastrow, 3) = Userform1.Controls("TB1" & i).Value
Cells(lastrow, 4) = Userform1.Controls("SNTB1" & i).Value
Cells(lastrow, 5) = Userform1.Controls("ReceiveTB" & i).Value
Next i
End If
ActiveWorkbook.Save
Call resetform
End Sub
Sub resetform()
RMATB.Value = ""
CustCB.Value = ""
TB1.Value = ""
SNTB1.Value = ""
ReceiveTB = ""
'sets focus on that first textbox again
RecForm.RMATB.SetFocus
One of our spreadsheets requires a userform. When trying to paste the user's values to the sheet housing the data, I get error code 13: type mismatch.
All the fields are textboxes. One line of code identical except the address of where we're posting the information works.
Here's what I have:
Public Sub btnSubmit_Click()
Dim TableSht As Worksheet
Dim NextRow As Long
Set TableSht = ThisWorkbook.Sheets("Table")
TableSht.Visible = True
'https://www.mrexcel.com/forum/excel-questions/1017033-making-all-fields-userform-mandatory.html#post4880848
'determine if any fields were left blank
For Each Control In Me.Controls '
Select Case TypeName(Control)
Case "TextBox"
If Control.Value = vbNullString Then
MsgBox "empty field in " & Control.Name
Exit For
End If
Case Else
End Select
Next Control
'data is housed in E3:J3, E5:J5, E7:J7, E9:J9. if statement determines what row information
'should be entered on.
If TableSht.Range("E3") = "" Then
NextRow = 3
ElseIf TableSht.Range("E5") = "" Then
NextRow = 5
ElseIf TableSht.Range("E7") = "" Then
NextRow = 7
ElseIf TableSht.Range("E9") = "" Then
NextRow = 9
Else
MsgBox ("There are no more available rows. Contact Craig for additional assistance.")
End If
'paste the user's data entry into the appropriate cells
With TableSht
.Cells(NextRow, 5) = Me.tbOwner
.Cells(NextRow, 6) = CDate(Me.tbDate)
.Cells(NextRow, 7) = Me.tbChange
'Me.tbChange.Value = CDec(Me.tbChange) 'no longer use this but one of my attempts
.Cells(NextRow, 8) = Me.tbAmount
.Cells(NextRow, 9) = Me.tbOriginal
.Cells(NextRow, 10) = Me.tbReason
.Cells(NextRow, 7).Value = Format(Range("G" & NextRow) / 100, "0.00%")
.Cells(NextRow, 8).Value = Format(Range("H" & NextRow), "$##.##")
.Cells(NextRow, 9).Value = Format(Range("I" & NextRow) / 100, "0.00%")
End With
Sheets("Rate Calculator v8").Select
TableSht.Visible = xlVeryHidden
Unload Me
End
End Sub
The error occurs on
.Cells(NextRow, 7).Value = Format(Range("G" & NextRow) / 100, "0.00%")
There's no error if I remove the line and cycle through the two after it, even though the last line before "end with" is essentially the same statement.
I've tried swapping the two lines of code that are similar. "Cells(NextRow, 7)..." and ".Cells(NextRow, 9)..." but the error still shows up on the "Cells(NextRow, 7)..." line.
I've confirmed the cells the data is pasted in columns G and I are both formatted as "percentage".
Qualify your Range usage with a sheet. If the sheet is also TableSht, the below should work. If the Range is from a different sheet, qualify that sheet
= Format(.Range("G" & NextRow) / 100, "0.00%")
= Format(.Range("H" & NextRow), "$##.##")
= Format(.Range("I" & NextRow) / 100, "0.00%")
I'm looking for help to get my Excel sheet into a specific format to import into Adwords. I have a sheet with data in this format:
I need to get it into this format:
What makes this tricky is getting 3 lines for each SKU. One line contains the Ad Group creation, the next is text ad creation and then the Keyword and bid is on the next line.
Can someone please help me achieve this? I would greatly appreciate it.
Thanks!
hope this helps...
(For all)
In a worksheet, set the range names that you can see the in code
ReportAddress
theSiteShort
theSiteLong
First
Second
And use it to reference inside the book. (This is just to not use $A$1)
Follow the comments...
'Take the report and store the address in the
'Range("ReportAddress") to use it later...
Sub takeReport()
'this is to take the reporte and store the address in Cells C6
'that has the name ReportAddress as you can see
Dim i
Dim FD As FileDialog 'to take files
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD 'invoke the file dialog
If .Show = -1 Then 'if you take any file (1 or more)
For Each i In .SelectedItems 'for each file do this...
Range("ReportAddress").Value = i 'and as you can see, just take one file...
Next i
End If
End With
End Sub
'to create the report
Sub do_Report()
Dim dirReport As String
Dim wrkData As Workbook
Dim shtData As Worksheet
Dim tmpReport As Workbook
Dim shtReport As Worksheet
Dim skuNum() 'to store the data
Dim skuName()
Dim vendorPartNum()
Dim manufacturer()
Dim r 'var for rows
Dim c
Dim col
Dim fil
Dim i
Dim counter
Dim shortSite As String
Dim longSite As String
Dim first As String
Dim second As String
'this is to a better handle of the data...
shortSite = Range("theSiteShort").Value
longSite = Range("theSiteLong").Value
first = Range("First").Value
second = Range("Second").Value
Workbooks.Open Range("ReportAddress").Value 'open the workbook with the data
Set wrkData = ActiveWorkbook
Set shtData = ActiveSheet 'here we can fail, because if the xls has more than 1 sheet and that sheet
'is not the sheet we need, could fail...
Workbooks.Add 'create a new workbook (CTRL+N)
Set tmpReport = ActiveWorkbook 'Store it here
Set shtReport = ActiveSheet 'as well the active sheet
'headlines
Range("A1").FormulaR1C1 = "Ad Group"
Range("B1").FormulaR1C1 = "Bid"
Range("C1").FormulaR1C1 = "Headline"
Range("D1").FormulaR1C1 = "Desc 1"
Range("E1").FormulaR1C1 = "Desc 2"
Range("F1").FormulaR1C1 = "Display URL"
Range("G1").FormulaR1C1 = "Final URL"
Range("H1").FormulaR1C1 = "Keyword"
wrkData.Activate 'got to the data report
shtData.Activate 'activate the sheet with the data, remember the comment!
r = Range("A1").End(xlDown).Row 'find the last row of data
c = Range("A1").End(xlToRight).Column 'As well the last column
For col = 1 To c 'well may is always the same qty of columns, but i like to count!!!
For fil = 2 To r 'for every row
Select Case col 'depends in which column is...
Case 1 'the first one use SkuNum... and so on... (This are the columns of the data source)
ReDim Preserve skuNum(1 To fil - 1)
skuNum(fil - 1) = Cells(fil, col)
Case 2
ReDim Preserve skuName(1 To fil - 1)
skuName(fil - 1) = Cells(fil, col)
Case 3
ReDim Preserve vendorPartNum(1 To fil - 1)
vendorPartNum(fil - 1) = Cells(fil, col)
Case 4
ReDim Preserve manufacturer(1 To fil - 1)
manufacturer(fil - 1) = Cells(fil, col)
Case Else
'do nothing 'just in case...
End Select
Next fil
Next col
tmpReport.Activate 'go to the new book, that is our final report
shtReport.Activate 'go to the sheet... just in case again... 'This line could be deletec
counter = 0 'a counter (index) for the vars()
For i = 1 To (r * 3) Step 3 '
'i got r lines and i need to put every 3 lines,
'then, that why I use Step 3 = (every 3th line), and that 3 * r.
counter = counter + 1
If counter > UBound(skuName) Then 'if the counter is bigger that the
'qty of vars inside SkuName (or any other)
Exit For 'get out the for loop!
End If
'here is the magic... almost...
Cells(i + 1, 1).Value = manufacturer(counter) & " - " & vendorPartNum(counter)
Cells(i + 1, 2).Value = first
Cells(i + 2, 1).Value = manufacturer(counter) & " - " & vendorPartNum(counter)
Cells(i + 2, 3).Value = manufacturer(counter) & " - " & vendorPartNum(counter) & " On Sale"
Cells(i + 2, 4).Value = skuName(counter)
Cells(i + 2, 5).Value = "Shop " & manufacturer(counter) & " now."
Cells(i + 2, 6).Value = shortSite & manufacturer(counter)
Cells(i + 2, 7).Value = longSite & skuNum(counter)
Cells(i + 3, 1).Value = manufacturer(counter) & " - " & vendorPartNum(counter)
Cells(i + 3, 2).Value = second
Cells(i + 3, 8).Value = "+" & manufacturer(counter) & " +" & vendorPartNum(counter)
Next i
Cells.EntireColumn.AutoFit 'Autofit all columns...
MsgBox "Ready!" 'Finish!
End Sub
I have a Word document based on a template for invoices and a database in Excel containing two tables: Company and Person.
I want to put some string into the textbox in the userform in Word which will then be searched in Excel. Excel shall return the values to a MultiColumn-Listbox located in another UserForm (this userform will only show if there is more than 1 result for the searched string).
This is the code I have in Word to run the macro, which actually gets started:
CSearchText = UFCompanySearch.tbSearchCompany.Value 'Textbox -> Search-String
xlWB.Application.Run("SearchCompany")
This only works when SearchCompany is a sub or a function with no further specifications, so
Function SearchCompany(SearchText As String)
doesn't work as I cannot run the Macro as follows:
xlWB.Application.Run("SearchCompany("SomeCompany")") 'NOTE!
NOTE: This will NOT work!!
To fill the Listbox in the UserForm I think there is the possibility to fill it with an Excel table, so this should somehow work out.
THIS is the PROBLEM:
I cannot refer to the Search-TextBox in the Userform which is located in the word Document as neither "Doc!" nor "Doc." works. Like this I can't search the cells for the string. This is the code I have to find cells containing the string:
IF (InStr(xlComp.Cells(Row, 1), CSearchText) > 0) Or _
(InStr(xlComp.Cells(Row, 2), CSearchText) > 0) Or _
(InStr(xlComp.Cells(Row, 3), CSearchText) > 0) Then
This searches the Columns A-C for the entered string. (Code I found somewhere... I have been searching too much to know where from ^.^)
Is there a way to address the UserForm in Word or a workaround to get the "SearchText" from the userform to Excel?
I'm quite new in VBA, so the more detailed your answer the more probable I will understand it.
As I did not find a way to do it directly I got a workaround when trying:
Code in Word:
Private Sub cbFirmaSearch_Click()
ActiveDocument.FormFields("FSearchText").Result = UFFirmaSearch.txtFirmaSuchen.Value
xlWB.Application.Run "SearchFirma"
ActiveDocument.FormFields("FSearchText").Delete
Dim DFLastRow As Integer
DFLastRow = xlWB.Sheets("DataFirma").Cells(xlWB.Sheets("DataFirma").Rows.Count, "a").End(xlUp).Row
Dim lbFirmTar As ListBox
Set lbFirmTar = UFFirmaSearchList.lbFirmaSearchList
Dim Row As Integer
For Row = 2 To DFLastRow
With lbFirmTar
Dim ListIndex As Integer
ListIndex = UFFirmaSearchList.lbFirmaSearchList.ListCount
.AddItem xlWB.Sheets("DataFirma").Cells(Row, 1).Value, ListIndex
.List(ListIndex, 1) = xlWB.Sheets("DataFirma").Cells(Row, 2).Value
.List(ListIndex, 2) = xlWB.Sheets("DataFirma").Cells(Row, 3).Value
.List(ListIndex, 3) = xlWB.Sheets("DataFirma").Cells(Row, 4).Value
.List(ListIndex, 4) = xlWB.Sheets("DataFirma").Cells(Row, 5).Value
.List(ListIndex, 5) = xlWB.Sheets("DataFirma").Cells(Row, 6).Value
.List(ListIndex, 6) = xlWB.Sheets("DataFirma").Cells(Row, 7).Value
End With
Next Row
With UFFirmaSearchList
If (.lbFirmaSearchList.ListCount > 1) Then
UFFirmaSearch.Hide
.Show
ElseIf (.lbFirmaSearchList.ListCount = 1) Then
FirmaID = .lbFirmaSearchList.List(0, 0)
FirmaZusatz = .lbFirmaSearchList.List(0, 1)
FirmaName = .lbFirmaSearchList.List(0, 2)
FirmaAbteilung = .lbFirmaSearchList.List(0, 3)
FirmaAdresse = .lbFirmaSearchList.List(0, 4)
FirmaPLZ = .lbFirmaSearchList.List(0, 5)
FirmaOrt = .lbFirmaSearchList.List(0, 6)
UFFirmaSearch.lblfrFirmenangaben = "Firma ID : " & FirmaID & _
"Firmenzusatz : " & FirmaZusatz & _
"Name : " & FirmaName & _
"Firmenabteilung : " & FirmaAbteilung & _
"Adresse : " & FirmaAdresse & _
"PLZ / Ort : " & FirmaPLZ & " " & FirmaOrt
Else
MsgBox "No Entry found.", vbOKOnly
End If
End With
UFFirmaSearch.txtFirmaSuchen.SetFocus
End Sub
Code in Excel:
Sub SearchFirma()
Dim Doc As Word.Document
Set Doc = ActiveDocument
Dim xlFirm As Worksheet
Set xlFirm = ActiveWorkbook.Sheets("Firma")
Dim LastRow As Integer 'Last row on sheet "Firma" containing values
LastRow = xlFirm.Cells(xlFirm.Rows.Count, "a").End(xlUp).Row
Dim xlDatFirm As Worksheet
Set xlDatFirm = ActiveWorkbook.Sheets("DataFirma")
Dim FSearchText As String
FSearchText = Doc.FormFields("FSearchText").Result
For Row = 2 To LastRow
Dim DFNewRow As Integer 'Next free line on sheet "DataFirma"
DFNewRow = xlDatFirm.Cells(xlDatFirm.Rows.Count, "A").End(xlUp).Row + 1
If (InStr(1, xlFirm.Cells(Row, 1), FSearchText, vbTextCompare) > 0) Or (InStr(1, xlFirm.Cells(Row, 2), FSearchText, vbTextCompare) > 0) Or (InStr(1, xlFirm.Cells(Row, 3).Value, FSearchText, vbTextCompare) > 0) Or (InStr(1, xlFirm.Cells(Row, 4).Value, FSearchText, vbTextCompare) > 0) Then
xlDatFirm.Range("A" & DFNewRow).Value = xlFirm.Cells(Row, 1).Value
xlDatFirm.Range("B" & DFNewRow).Value = xlFirm.Cells(Row, 2).Value
xlDatFirm.Range("C" & DFNewRow).Value = xlFirm.Cells(Row, 3).Value
xlDatFirm.Range("D" & DFNewRow).Value = xlFirm.Cells(Row, 4).Value
xlDatFirm.Range("E" & DFNewRow).Value = xlFirm.Cells(Row, 5).Value
xlDatFirm.Range("F" & DFNewRow).Value = xlFirm.Cells(Row, 6).Value
xlDatFirm.Range("G" & DFNewRow).Value = xlFirm.Cells(Row, 7).Value
End If
Next Row
End Sub
Somehow this works. When I first tried "Dim xlWB As Excel.Workbook" in Word I would always get a runtime error. When I tried "Dim Doc As Word.Document" in Excel though I never got an error... very strange but still somehow managed to get it over with.
If you have any questions regarding this I will be happy to try to help and if there are things that I can rewrite in a better way, please don't hesitate to comment.
Thanks for the support :)