VBA宏编译错误

时间:2017-08-03 16:35:20

标签: excel vba excel-vba sum range

我尝试编写一个简单的VBA宏,它将使用活动单元格的列和用户的输入在一行中添加一系列单元格。通过将用户输入的整数添加到活动列并且即结束列来计算范围。问题是它给了我一个"编译错误:无效的限定符"当我运行它,并对总数'生气线。

这是我的代码。我刚从VBA开始,但它不会那么难......对吗?

[Route("api/PostUserImage")]
    [AllowAnonymous]
    public async Task<HttpResponseMessage> PostUserImage()
    {
        Dictionary<string, object> dict = new Dictionary<string, object>();
        try
        {

            var httpRequest = HttpContext.Current.Request;

            foreach (string file in httpRequest.Files)
            {
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);

                var postedFile = httpRequest.Files[file];
                if (postedFile != null && postedFile.ContentLength > 0)
                {

                    int MaxContentLength = 1024 * 1024 * 1; //Size = 1 MB  

                    IList<string> AllowedFileExtensions = new List<string> { ".jpg", ".gif", ".png" };
                    var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
                    var extension = ext.ToLower();
                    if (!AllowedFileExtensions.Contains(extension))
                    {

                        var message = string.Format("Please Upload image of type .jpg,.gif,.png.");

                        dict.Add("error", message);
                        return Request.CreateResponse(HttpStatusCode.BadRequest, dict);
                    }
                    else if (postedFile.ContentLength > MaxContentLength)
                    {

                        var message = string.Format("Please Upload a file upto 1 mb.");

                        dict.Add("error", message);
                        return Request.CreateResponse(HttpStatusCode.BadRequest, dict);
                    }
                    else
                    {

                        var date = DateTime.Now.ToString();

                        var filePath = HttpContext.Current.Server.MapPath("~/Image/" + postedFile.FileName+date+ extension);

                        postedFile.SaveAs(filePath);

                    }
                }

                var message1 = string.Format("Image Updated Successfully.");
                return Request.CreateErrorResponse(HttpStatusCode.Created, message1); ;
            }
            var res = string.Format("Please Upload a image.");
            dict.Add("error", res);
            return Request.CreateResponse(HttpStatusCode.NotFound, dict);
        }
        catch (Exception ex)
        {
            var res = string.Format("some Message");
            dict.Add("error", res);
            return Request.CreateResponse(HttpStatusCode.NotFound, dict);
        }
    }

1 个答案:

答案 0 :(得分:0)

看起来像语法错误。尝试删除.valuemonth上的total

Sub Food()
Dim first As Variant
Dim last As Integer
Dim days As Integer
Dim month As Range
Dim total As Double
first = ActiveCell.Column
days = InputBox("Days in the month?")
last = first + days
Set month = Range(Cells(first, 4), Cells(last, 4))
total = WorksheetFunction.Sum(month)
Worksheets(1).Cells(1, 13).Value = total
End Sub

如果要将范围的值转换为变量,则将.value与范围而不是变量放在一起。例如:

x = cells(1,2).value

.value属性返回范围对象中的值。因此,在函数上使用它并没有意义,如果你尝试,会导致excel抛出错误。尝试将month变量设置为值而不是范围也没有意义,因为这只会使它成为一个数组。如果您希望month成为数组,则需要将其设置为变体而不是范围。

有关.value属性的更多信息,请参阅以下链接: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-value-property-excel