riteshjain
New Member
- Joined
- Aug 11, 2015
- Messages
- 2
I am new in VBA, For finding values from multiple sheets (With sheet name) i have written code as follow, but it takes too much time to get out put in 8-9 cells in a one sheet.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim temp
Dim cs
Dim ts
Dim var1
'Target.Worksheets("Overview").Range("C8").Value
If Target.Address = "$C$8" Then
'Assign variables for Current Sheet, Target Sheet and search text
cs = ActiveSheet.Name
ts = "Overview"
var1 = Sheets(ts).Range("C8").Value
var2 = Sheets(ts).Range("C6").Value
var3 = Sheets(ts).Range("B9").Value
var4 = Sheets(ts).Range("B10").Value
var5 = Sheets(ts).Range("B11").Value
var6 = Sheets(ts).Range("B12").Value
var7 = Sheets(ts).Range("B13").Value
var8 = Sheets(ts).Range("B14").Value
var9 = Sheets(ts).Range("B15").Value
'Loop through all worksheets except listed
Application.ScreenUpdating = False
For Each wks In ActiveWorkbook.Worksheets
'Do this for all sheets except these
Select Case wks.Name
Case ts, "Input"
'do nothing with the above worksheets
Case Else
'With worksheets not listed, do the following...
With wks
Sheets(wks.Name).Activate
Set Source = ActiveSheet.Range("A6:AA4500")
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var2 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "C16" in Sheet1
Sheets(ts).Range("D16").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For component name
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var3 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D9" in Sheet1
Sheets(ts).Range("D9").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For Drawing no.
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var4 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D10" in Sheet1
Sheets(ts).Range("D10").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For Standard
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var5 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D11" in Sheet1
Sheets(ts).Range("D11").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For DN
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var6 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D11" in Sheet1
Sheets(ts).Range("D12").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For Dimension
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var7 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D11" in Sheet1
Sheets(ts).Range("D13").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For SCH
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var8 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D11" in Sheet1
Sheets(ts).Range("D14").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For material
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var9 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D11" in Sheet1
Sheets(ts).Range("D15").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
End With
End Select
Next wks
Sheets(cs).Activate
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim temp
Dim cs
Dim ts
Dim var1
'Target.Worksheets("Overview").Range("C8").Value
If Target.Address = "$C$8" Then
'Assign variables for Current Sheet, Target Sheet and search text
cs = ActiveSheet.Name
ts = "Overview"
var1 = Sheets(ts).Range("C8").Value
var2 = Sheets(ts).Range("C6").Value
var3 = Sheets(ts).Range("B9").Value
var4 = Sheets(ts).Range("B10").Value
var5 = Sheets(ts).Range("B11").Value
var6 = Sheets(ts).Range("B12").Value
var7 = Sheets(ts).Range("B13").Value
var8 = Sheets(ts).Range("B14").Value
var9 = Sheets(ts).Range("B15").Value
'Loop through all worksheets except listed
Application.ScreenUpdating = False
For Each wks In ActiveWorkbook.Worksheets
'Do this for all sheets except these
Select Case wks.Name
Case ts, "Input"
'do nothing with the above worksheets
Case Else
'With worksheets not listed, do the following...
With wks
Sheets(wks.Name).Activate
Set Source = ActiveSheet.Range("A6:AA4500")
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var2 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "C16" in Sheet1
Sheets(ts).Range("D16").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For component name
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var3 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D9" in Sheet1
Sheets(ts).Range("D9").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For Drawing no.
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var4 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D10" in Sheet1
Sheets(ts).Range("D10").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For Standard
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var5 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D11" in Sheet1
Sheets(ts).Range("D11").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For DN
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var6 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D11" in Sheet1
Sheets(ts).Range("D12").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For Dimension
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var7 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D11" in Sheet1
Sheets(ts).Range("D13").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For SCH
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var8 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D11" in Sheet1
Sheets(ts).Range("D14").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
'For material
For Each Row In Source
On Error Resume Next
If Row.Value = var1 Then
For Each Col In Source
If Col.Value = var9 Then
temp = Col.Column()
End If
Next Col
'Vlookup code a cell "D11" in Sheet1
Sheets(ts).Range("D15").Value = Application.WorksheetFunction.VLookup(var1, wks.Range("A6:AA4500"), temp, False)
End If
Next Row
End With
End Select
Next wks
Sheets(cs).Activate
End If
End Sub