I have 3 versions of a script I run inside VBA in Module1 of an excel sheet dependent on how raw data is received from a customer. The data can be organized in 1 of 3 manners below:
Data Type 1: R1, R2, R3, R4, R5
Data Type 2: R1,R2,R3,R4,R5
Data Type 3: R1 R2 R3 R4 R5
Essentially, the separating factor is a comma and a space, a comma only, or a space only. I have 3 code segments written for each case, but would like to combine all of the codes into 1 for ease of use. Theoretically Type 1 needs to be checked for 1st, then Type 2, then Type 3 as that order should eliminate false positives and formatting errors. I have tried adding an "If" statement to capture this but have been unsuccessful so far.
The part of the code I have been trying to edit is the following, specifically the "arrRef" portion:
Type 1
Type 2
Type 3
The codes I have written in entirety will be placed below. For a description of how this code was originally generated and what its use is for, please reference this thread: http://www.mrexcel.com/forum/excel-questions/643373-complex-script-question.html#post3192793
The main difference is I run this script form a module as opposed to the "ThisWorkbook" area of the VBA project as bertie instructed due to having a different code in "ThisWorkbook" to force pasting of values only across the whole sheet.
Here are the codes in entirety:
Type 1 - Comma and Space
Type 2 - Comma Only
Type 3 - Space Only
I am by no means a VBA expert of any kind. I have some programming experience and can pick things up relatively well when seeing examples, but this one I am really needing some help on.
Please feel free to ask any questions you might have and I will try to answer to the best of my ability.
Thanks in advance.
I am unsure if this will be needed info, but just so the info is here, the code I have in "ThisWorkbook" of the same excel document is from this thread: http://www.mrexcel.com/forum/excel-questions/230718-force-paste-special-values-2.html
Data Type 1: R1, R2, R3, R4, R5
Data Type 2: R1,R2,R3,R4,R5
Data Type 3: R1 R2 R3 R4 R5
Essentially, the separating factor is a comma and a space, a comma only, or a space only. I have 3 code segments written for each case, but would like to combine all of the codes into 1 for ease of use. Theoretically Type 1 needs to be checked for 1st, then Type 2, then Type 3 as that order should eliminate false positives and formatting errors. I have tried adding an "If" statement to capture this but have been unsuccessful so far.
The part of the code I have been trying to edit is the following, specifically the "arrRef" portion:
Type 1
Code:
Do Until RefDesData = ""
'spilit into the array on comma followed by space
arrRef = Split(RefDesData.Value, ", ")
Type 2
Code:
Do Until RefDesData = ""
'spilit into the array on comma only
arrRef = Split(RefDesData.Value, ",")
Type 3
Code:
Do Until RefDesData = ""
'spilit into the array on space only
arrRef = Split(RefDesData.Value, " ")
The codes I have written in entirety will be placed below. For a description of how this code was originally generated and what its use is for, please reference this thread: http://www.mrexcel.com/forum/excel-questions/643373-complex-script-question.html#post3192793
The main difference is I run this script form a module as opposed to the "ThisWorkbook" area of the VBA project as bertie instructed due to having a different code in "ThisWorkbook" to force pasting of values only across the whole sheet.
Here are the codes in entirety:
Type 1 - Comma and Space
Code:
Option Explicit
Sub Main()
Dim RefDesData As Range
Dim PnPData As Worksheet
Dim arrRef As Variant 'array of references
Dim i As Long 'loop variable
Dim strTemp As String
Set RefDesData = Sheets("SortedRefDes").Range("B3")
Set PnPData = Sheets("PnP")
On Error GoTo errExit
Do Until RefDesData = ""
'spilit into the array on comma followed by space
arrRef = Split(RefDesData.Value, ", ")
For i = LBound(arrRef) To UBound(arrRef)
strTemp = FindRef(PnPData, Trim(arrRef(i)))
'output
Select Case UCase(strTemp)
Case "TOP"
If RefDesData.Offset(, 1).Value = "" Then
RefDesData.Offset(, 1).Value = arrRef(i)
Else
RefDesData.Offset(, 1).Value = RefDesData.Offset(, 1).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 2).Value = RefDesData.Offset(, 2).Value + 1
Case "T"
If RefDesData.Offset(, 1).Value = "" Then
RefDesData.Offset(, 1).Value = arrRef(i)
Else
RefDesData.Offset(, 1).Value = RefDesData.Offset(, 1).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 2).Value = RefDesData.Offset(, 2).Value + 1
Case "BOTTOM"
If RefDesData.Offset(, 3).Value = "" Then
RefDesData.Offset(, 3).Value = arrRef(i)
Else
RefDesData.Offset(, 3).Value = RefDesData.Offset(, 3).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 4).Value = RefDesData.Offset(, 4).Value + 1
Case "B"
If RefDesData.Offset(, 3).Value = "" Then
RefDesData.Offset(, 3).Value = arrRef(i)
Else
RefDesData.Offset(, 3).Value = RefDesData.Offset(, 3).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 4).Value = RefDesData.Offset(, 4).Value + 1
Case Else
'not found
'code for ref not found goes here
End Select
'calculate total
RefDesData.Offset(, 5).Value = _
RefDesData.Offset(, 2).Value + RefDesData.Offset(, 4).Value
Next i
'next row
Set RefDesData = RefDesData.Offset(1, 0)
Loop
errExit:
'tidy up and release memory
Set RefDesData = Nothing
Set PnPData = Nothing
End Sub
Private Function FindRef(ByVal ws As Worksheet, _
ByVal ref As String) As String
Dim rngFound As Range
On Error Resume Next
With ws
Set rngFound = .Columns(1).Find(What:=ref, _
After:=.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
On Error GoTo 0
End With
If Not rngFound Is Nothing Then
FindRef = rngFound.Offset(, 1).Value
Else
FindRef = ""
End If
End Function
Type 2 - Comma Only
Code:
Option Explicit
Sub Main()
Dim RefDesData As Range
Dim PnPData As Worksheet
Dim arrRef As Variant 'array of references
Dim i As Long 'loop variable
Dim strTemp As String
Set RefDesData = Sheets("SortedRefDes").Range("B3")
Set PnPData = Sheets("PnP")
On Error GoTo errExit
Do Until RefDesData = ""
'spilit into the array on comma only
arrRef = Split(RefDesData.Value, ",")
For i = LBound(arrRef) To UBound(arrRef)
strTemp = FindRef(PnPData, Trim(arrRef(i)))
'output
Select Case UCase(strTemp)
Case "TOP"
If RefDesData.Offset(, 1).Value = "" Then
RefDesData.Offset(, 1).Value = arrRef(i)
Else
RefDesData.Offset(, 1).Value = RefDesData.Offset(, 1).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 2).Value = RefDesData.Offset(, 2).Value + 1
Case "T"
If RefDesData.Offset(, 1).Value = "" Then
RefDesData.Offset(, 1).Value = arrRef(i)
Else
RefDesData.Offset(, 1).Value = RefDesData.Offset(, 1).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 2).Value = RefDesData.Offset(, 2).Value + 1
Case "BOTTOM"
If RefDesData.Offset(, 3).Value = "" Then
RefDesData.Offset(, 3).Value = arrRef(i)
Else
RefDesData.Offset(, 3).Value = RefDesData.Offset(, 3).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 4).Value = RefDesData.Offset(, 4).Value + 1
Case "B"
If RefDesData.Offset(, 3).Value = "" Then
RefDesData.Offset(, 3).Value = arrRef(i)
Else
RefDesData.Offset(, 3).Value = RefDesData.Offset(, 3).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 4).Value = RefDesData.Offset(, 4).Value + 1
Case Else
'not found
'code for ref not found goes here
End Select
'calculate total
RefDesData.Offset(, 5).Value = _
RefDesData.Offset(, 2).Value + RefDesData.Offset(, 4).Value
Next i
'next row
Set RefDesData = RefDesData.Offset(1, 0)
Loop
errExit:
'tidy up and release memory
Set RefDesData = Nothing
Set PnPData = Nothing
End Sub
Private Function FindRef(ByVal ws As Worksheet, _
ByVal ref As String) As String
Dim rngFound As Range
On Error Resume Next
With ws
Set rngFound = .Columns(1).Find(What:=ref, _
After:=.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
On Error GoTo 0
End With
If Not rngFound Is Nothing Then
FindRef = rngFound.Offset(, 1).Value
Else
FindRef = ""
End If
End Function
Type 3 - Space Only
Code:
Option Explicit
Sub Main()
Dim RefDesData As Range
Dim PnPData As Worksheet
Dim arrRef As Variant 'array of references
Dim i As Long 'loop variable
Dim strTemp As String
Set RefDesData = Sheets("SortedRefDes").Range("B3")
Set PnPData = Sheets("PnP")
On Error GoTo errExit
Do Until RefDesData = ""
'spilit into the array on space only
arrRef = Split(RefDesData.Value, " ")
For i = LBound(arrRef) To UBound(arrRef)
strTemp = FindRef(PnPData, Trim(arrRef(i)))
'output
Select Case UCase(strTemp)
Case "TOP"
If RefDesData.Offset(, 1).Value = "" Then
RefDesData.Offset(, 1).Value = arrRef(i)
Else
RefDesData.Offset(, 1).Value = RefDesData.Offset(, 1).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 2).Value = RefDesData.Offset(, 2).Value + 1
Case "T"
If RefDesData.Offset(, 1).Value = "" Then
RefDesData.Offset(, 1).Value = arrRef(i)
Else
RefDesData.Offset(, 1).Value = RefDesData.Offset(, 1).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 2).Value = RefDesData.Offset(, 2).Value + 1
Case "BOTTOM"
If RefDesData.Offset(, 3).Value = "" Then
RefDesData.Offset(, 3).Value = arrRef(i)
Else
RefDesData.Offset(, 3).Value = RefDesData.Offset(, 3).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 4).Value = RefDesData.Offset(, 4).Value + 1
Case "B"
If RefDesData.Offset(, 3).Value = "" Then
RefDesData.Offset(, 3).Value = arrRef(i)
Else
RefDesData.Offset(, 3).Value = RefDesData.Offset(, 3).Value & "," & arrRef(i)
End If
RefDesData.Offset(, 4).Value = RefDesData.Offset(, 4).Value + 1
Case Else
'not found
'code for ref not found goes here
End Select
'calculate total
RefDesData.Offset(, 5).Value = _
RefDesData.Offset(, 2).Value + RefDesData.Offset(, 4).Value
Next i
'next row
Set RefDesData = RefDesData.Offset(1, 0)
Loop
errExit:
'tidy up and release memory
Set RefDesData = Nothing
Set PnPData = Nothing
End Sub
Private Function FindRef(ByVal ws As Worksheet, _
ByVal ref As String) As String
Dim rngFound As Range
On Error Resume Next
With ws
Set rngFound = .Columns(1).Find(What:=ref, _
After:=.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
On Error GoTo 0
End With
If Not rngFound Is Nothing Then
FindRef = rngFound.Offset(, 1).Value
Else
FindRef = ""
End If
End Function
I am by no means a VBA expert of any kind. I have some programming experience and can pick things up relatively well when seeing examples, but this one I am really needing some help on.
Please feel free to ask any questions you might have and I will try to answer to the best of my ability.
Thanks in advance.
I am unsure if this will be needed info, but just so the info is here, the code I have in "ThisWorkbook" of the same excel document is from this thread: http://www.mrexcel.com/forum/excel-questions/230718-force-paste-special-values-2.html
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim UndoString As String
Dim srce As Range
On Error GoTo err_handler
UndoString = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Left(UndoString, 5) <> "Paste" And UndoString <> "Auto Fill" Then
Exit Sub
End If
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Undo
If UndoString = "Auto Fill" Then
Set srce = Selection
srce.Copy
Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.SendKeys "{ESC}"
Union(Target, srce).Select
Else
Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
err_handler:
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Last edited: