当时还使用on、 s、ado、或另一种语言或微粒,用on或西sim,用直截了当的本地方式盘问封闭式工作手册(在关闭时打上一本工作手册)。 但是,我们认为我们可以开设一个工作手册inviible。 你在(”时暗示,可以开诚布公,但藏匿(或实际上不展示)一本工作书?
Sub FindInClosedWorkbookpartmanyallXLSB()
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Dim searchStr As String
Dim foundCell As Range
Dim firstAddress As String
Dim addresses As String
Set the string you are searching for
searchStr = "S"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Open the workbook invisibly
Set wb = Workbooks.Open("C:UserskarmaDocumentsworkbook test to search.xlsb", ReadOnly:=True, UpdateLinks:=False)
wb.Windows(1).Visible = False
Set the worksheet you want to search in
Set ws = wb.Worksheets("Sheet1")
Search in column G i know you asked for D:D
Set rng = ws.Range("G:G")
Use the Find method to search for the string
Set foundCell = rng.Find(What:=searchStr, LookIn:=xlValues, LookAt:=xlPart)
If Not foundCell Is Nothing Then
Save the address of the first found cell
firstAddress = foundCell.Address
Do
Add the address to the addresses string
addresses = addresses & foundCell.Address & vbCrLf
Continue searching
Set foundCell = rng.FindNext(after:=foundCell)
Loop while we have not returned to the first found cell
Loop While Not foundCell Is Nothing And foundCell.Address <> firstAddress
Display a single message box with all addresses
MsgBox "Found " & searchStr & " at: " & vbCrLf & addresses
Else
MsgBox searchStr & " not found."
End If
Close the workbook you were searching without saving
wb.Close SaveChanges:=False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
我的榜样是用Xlpart在G栏中搜索任何座标,S 。 但是,如果你想要的话,你可以回头看血管和单一结果(通过拆除 lo)。
如果你想要在宏观运行的手册表1中取得结果,你可以这样做:
Dim wb thisWb As Workbook
Dim ws thisWs As Worksheet
页: 1
Set the workbook and worksheet where the macro resides
Set thisWb = ThisWorkbook
Set thisWs = thisWb.Sheets("Sheet1")
页: 1
Loop while we have not returned to the first found cell
Loop While Not foundCell Is Nothing And foundCell.Address <> firstAddress
Else
thisWs.Cells(rowNumber, "I").Value = searchStr & " not found."
End If
Close the workbook you were searching without saving
wb.Close SaveChanges:=False
-------- 2
with python though it can be done alot easier:
<> 全文> 计算机:
##
import pandas as pd
from pyxlsb import open_workbook as open_xlsb
# Set the string you are searching for
searchStr = S
df = []
with open_xlsb(r C:UserskarmaDocumentsworkbook test to search.xlsb ) as wb:
with wb.get_sheet(1) as sheet: # Sheet index start at 1
for row in sheet.rows():
df.append([item.v for item in row]) # .v to fetch the value of the cell
df = pd.DataFrame(df[1:], columns=df[0]) # df[0] contains column names
results = df.isin([searchStr])
if results.any().any(): # .any().any() checks if there s any True in the entire DataFrame
print(f Found "{searchStr}" at: )
series = results.unstack() # this converts the DataFrame to a multi-index Series
for idx, value in series.items():
if value:
print(f Row {idx[1]}, Column {idx[0]} ) # idx is a tuple with (column, row)
else:
print(f "{searchStr}" not found. )
For partial matches anywhere in thw sheet workbook or part of a cell:
import pandas as pd
from pyxlsb import open_workbook as open_xlsb
# Set the string you are searching for
searchStr = S
df = []
with open_xlsb(r C:UserskarmaDocumentsworkbook test to search.xlsb ) as wb:
with wb.get_sheet(1) as sheet: # Sheet index start at 1
for row in sheet.rows():
df.append([item.v for item in row]) # .v to fetch the value of the cell
df = pd.DataFrame(df[1:], columns=df[0]) # df[0] contains column names
mask = df.applymap(lambda x: searchStr in str(x)) # applymap works element-wise on a DataFrame
if mask.any().any(): # .any().any() checks if there s any True in the entire DataFrame
print(f Found "{searchStr}" at: )
series = mask.unstack() # this converts the DataFrame to a multi-index Series
for idx, value in series.items():
if value:
print(f Row {idx[1]}, Column {idx[0]} ) # idx is a tuple with (column, row)
else:
print(f "{searchStr}" not found. )
你们可以看到,你要求解决的问题可以解决,而且只能靠本土做,似乎无情地用沙捞。 它是本土的。
but with and for VBA , best we could do is mimick not opening the file , and doing so "invisibly" in combo with what Tim Williams said .
Something cool from/using cmd (To look really cool you could query from CMD you could save a script like this and do all the calling from cmd).
import pandas as pd
from pyxlsb import open_workbook as open_xlsb
import argparse
# Create the parser and add arguments
parser = argparse.ArgumentParser()
parser.add_argument("--search", help="The string to search for", required=True)
parser.add_argument("--file", help="The path to the .xlsb file", required=True)
args = parser.parse_args()
# Extract arguments
searchStr = args.search
filePath = args.file
# Open the xlsb file
with open_xlsb(filePath) as wb:
for sheet_name in wb.sheets: # iterate through each sheet
df = []
# Read the sheet into a DataFrame
with wb.get_sheet(sheet_name) as sheet:
for row in sheet.rows():
df.append([item.v for item in row])
df = pd.DataFrame(df[1:], columns=df[0])
# Iterate over each column
for column_name in df.columns:
# Convert the column to string type and check if it contains the search string
# Fill NA/NaN values with an empty string before converting to string
column_as_str = df[column_name].fillna( ).astype(str)
# Check if it contains the search string
mask = column_as_str.str.contains(searchStr)
# If the string is found in the column
if mask.any():
print(f Found "{searchStr}" in sheet "{sheet_name}" at: )
# Get the row indices where the string is found
row_indices = column_as_str[mask].index.values
for row_idx in row_indices:
print(f Row {row_idx + 1}, Column "{column_name}" )
else:
print(f "{searchStr}" not found in sheet "{sheet_name}", column "{column_name}" )
• 在排雷中心进行指挥:
C:Windowssystem32>python C:Users...path..program_name.py --search "THE STRING YOUR SEARCHING FOR GOES HERE" --file "C:Users..PATH..YOUR FILE.xlsb"