我正在尝试创建一个试图执行以下逻辑的应用程序
循环遍历工作表中包含产品代码和类别的每一行。 wkScrap持有参考 到临时/废料工作单。
create function [dbo].[fn_StringSplit4k]
(
@str nvarchar(4000) = ' ' -- String to split.
,@delimiter as nvarchar(1) = ',' -- Delimiting value to split on.
,@num as int = null -- Which value to return, null returns all.
)
returns table
as
return
-- Start tally table with 10 rows.
with n(n) as (select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1)
-- Select the same number of rows as characters in @str as incremental row numbers.
-- Cross joins increase exponentially to a max possible 10,000 rows to cover largest @str length.
,t(t) as (select top (select len(isnull(@str,'')) a) row_number() over (order by (select null)) from n n1,n n2,n n3,n n4)
-- Return the position of every value that follows the specified delimiter.
,s(s) as (select 1 union all select t+1 from t where substring(isnull(@str,''),t,1) = @delimiter)
-- Return the start and length of every value, to use in the SUBSTRING function.
-- ISNULL/NULLIF combo handles the last value where there is no delimiter at the end of the string.
,l(s,l) as (select s,isnull(nullif(charindex(@delimiter,isnull(@str,''),s),0)-s,4000) from s)
select rn
,item
from(select row_number() over(order by s) as rn
,substring(@str,s,l) as item
from l
) a
where rn = @num
or @num is null;
我面临的问题是当我试图保存catWorkBook时,我得到了一个
对象_workbook的方法saveas失败
错误。以下是我用来保存excel文件的方法。
wkScrap.Activate
lProdRow = Cells(rows.Count, 1).End(xlUp).Row
'loop through each row and get the product id and the category
'ignore first row, it is the header
Dim catWorkBook As Workbook
oProductCat = "0" 'the initial value set as 0, which would NEVER be a category
With wkScrap
For i = 2 To lProdRow
cProductCode = .Cells(i, 1).Value
cProductCat = .Cells(i, 2).Value
'will need to open a category specific .xls file
If (StrComp(cProductCat, oProductCat) <> 0) Then
'save the existing workbook
FileIO.CloseExcelFile oProductCat, catWorkBook
Set catWorkBook = Nothing 'clear all traces of the old worksheet
Set catWorkBook = CreateBlankWorkBook()
MsgBox (cProductCat + " " + catWorkBook.Name)
oProductCat = cProductCat
End If
'catWorkBook.Save 'we keep on saving any ways.
Next
End With
为什么我收到此错误?是因为我在工作表中循环,同时尝试激活并保存另一张工作表吗?
PS:我还没有编写逻辑来将特定类别的产品实际移动到新创建的Excel工作表中。
答案 0 :(得分:0)
问题已经解决。这是忙碌的一天,我在子程序CloseExcelFile中犯了两个错误
错误#1
onlyFileName = fileName + ext
FileName甚至没有在程序中定义。文件名必须是category(catCode)和.xlsx扩展名的组合。
错误#2
qualifiedFileName = salesReportPath + slash + onlyFileName
我假设salesReportPath始终是有效路径。但事实并非如此。如果我遵循运行宏的特定模式,它只会被设置。我也纠正了这一点。
@Imran Malek,@ destination-data 17:你的问题让我重新检查代码,这也帮助我找到答案。感谢!!!