PritishS
Board Regular
- Joined
- Dec 29, 2015
- Messages
- 119
- Office Version
- 2007
- Platform
- Windows
Hi,
I'm trying to Search a string in a Dictionary and if found replace it with values from text boxes in all worksheets.
I'm attaching a sample workbook with code for further reference.
What I have tried yet-
1. I have a workbook with many worksheets (around 2000). But in my sample file 3 sheets are there.
2. Each sheet have some data in of pens of different colors.
3. My objective is to consider a pen data to replace with another pen data in entire workbook. For example - I want to replace 'Red color ball point pen' with 'Green color ball point pen' in all worksheets.
4. What I'm doing with my code-
a. Once you click on 'Replace' Button, one userform will open
b. As I have selected 'Red color ball point pen' at row 5, on userform initialize I'm showing selected row number in point-1.
c. After that I'm clicking on 'Get String' button (point-2) to make string in Textbox (point-3).
d. Next I'm selecting row number 6, because I want to replace it with 'Green color ball point pen'. So in Point-4 its the selected row number.
e. Next I'll click on 'Relpace with' button (point-5), which will show data of that selected row (A col data in 1st Textbox, B col data=2nd textbox, C col data = 3rd textbox and D col data =4th textbox.) (Point -6).
f. Next by clicking on 'Replace' button (point-7), I'm trying to search the string 'Red color ball point pen,XYA,RPEN-B' in all worksheets and if found then replace that rows Col-A,B,C & D with
textbox values of Point-6.
e. By clicking on 'Replace' button, I'm creating a dictionary of all item available in sheet1. But unable to compare and replace the found string row with desired value.
Thanks in advance for looking into it.
Sample File with VBA
Meet Google Drive – One place for all your files
Userform VBA I have tried-
Thanks & Regrads,
PritishS
I'm trying to Search a string in a Dictionary and if found replace it with values from text boxes in all worksheets.
I'm attaching a sample workbook with code for further reference.
What I have tried yet-
1. I have a workbook with many worksheets (around 2000). But in my sample file 3 sheets are there.
2. Each sheet have some data in of pens of different colors.
3. My objective is to consider a pen data to replace with another pen data in entire workbook. For example - I want to replace 'Red color ball point pen' with 'Green color ball point pen' in all worksheets.
4. What I'm doing with my code-
a. Once you click on 'Replace' Button, one userform will open
b. As I have selected 'Red color ball point pen' at row 5, on userform initialize I'm showing selected row number in point-1.
c. After that I'm clicking on 'Get String' button (point-2) to make string in Textbox (point-3).
d. Next I'm selecting row number 6, because I want to replace it with 'Green color ball point pen'. So in Point-4 its the selected row number.
e. Next I'll click on 'Relpace with' button (point-5), which will show data of that selected row (A col data in 1st Textbox, B col data=2nd textbox, C col data = 3rd textbox and D col data =4th textbox.) (Point -6).
f. Next by clicking on 'Replace' button (point-7), I'm trying to search the string 'Red color ball point pen,XYA,RPEN-B' in all worksheets and if found then replace that rows Col-A,B,C & D with
textbox values of Point-6.
e. By clicking on 'Replace' button, I'm creating a dictionary of all item available in sheet1. But unable to compare and replace the found string row with desired value.
Thanks in advance for looking into it.
Sample File with VBA
Meet Google Drive – One place for all your files
Userform VBA I have tried-
VBA Code:
Option Explicit
Private Sub CommandButton1_Click()
Dim str As String
With Application
str = ActiveCell.Value & "," & ActiveCell.Offset(, 1).Value & "," & ActiveCell.Offset(, 2).Value
End With
Me.TextBox2.Value = str
End Sub
Private Sub CommandButton2_Click()
Me.TextBox3.Value = ActiveCell.Row
Me.TextBox4.Value = Range("A" & ActiveCell.Row).Value
Me.TextBox5.Value = Range("B" & ActiveCell.Row).Value
Me.TextBox6.Value = Range("C" & ActiveCell.Row).Value
Me.TextBox7.Value = Range("D" & ActiveCell.Row).Value
End Sub
Private Sub CommandButton3_Click()
Dim rng As Range, Dn As Range, n As Long, str As String, str1 As String
Dim sht As Worksheet, Q As Variant, Dic As Object, r As Range
Dim answer As Integer, RngD As Range
Set Dic = CreateObject("scripting.dictionary")
Dic.comparemode = vbTextCompare
str1 = Me.TextBox2.Value
With ActiveSheet
Set rng = .Range(.Range("A5"), .Range("A" & Rows.Count).End(xlUp))
End With
For Each Dn In rng
With Application
str = Dn.Value & "," & Dn.Offset(, 1).Value & "," & Dn.Offset(, 2).Value
End With
If Not Dic.Exists(str) Then
Dic.Add (str), Array(Dn, 3)
Else
Q = Dic(str)
Set Q(0) = Union(Q(0), Dn)
Dic(str) = Q
End If
Next Dn
' '''''''After this i don't have a clue how to proceed.
End Sub
Private Sub UserForm_Initialize()
Me.TextBox1.Value = ActiveCell.Row
End Sub
Thanks & Regrads,
PritishS